0

I have a folder full of XML files. I need to get the node value of the same node in each, and then write this value to a single text file as a list.

In other words, every one of these files has a node. I need a list of all the userid value in one text file.

I don't need a process, as this will only be done once, so I was trying to figure out a shell script I could run on the folder of files (I am on OSX/Unix).

1
  • 4
    An example of the input and format of the desired output would help.CommentedAug 4, 2011 at 15:17

1 Answer 1

1

You can use perl for this, lets say these are your files

$ ls xml/*.xml xml/1312537177.xml xml/1312537179.xml xml/1312537182.xml 

and this is the content

$ cat xml/*.xml <?xml version="1.0" encoding="UTF-8" ?> <somenode><userid>1312537177</userid><otherstuff>fsadfadsf</otherstuff></somenode> <?xml version="1.0" encoding="UTF-8" ?> <somenode><userid>1312537179</userid><otherstuff>fsadfadsf</otherstuff></somenode> <?xml version="1.0" encoding="UTF-8" ?> <somenode><userid>1312537182</userid><otherstuff>fsadfadsf</otherstuff></somenode> 

using this simple bash command

$ for i in $(ls xml/*.xml); do cat $i | perl -M'XML::Bare' -e "local( $/ ); print ( (new XML::Bare(text => <STDIN> ))->parse()->{'somenode'}{'userid'}{'value'});print \"\n\""; done; 1312537177 1312537179 1312537182 

you can get the contents of the userid node.

Of course you need to change the command to fit your xml structure.

Note that you might need to install the XML::Bare perl module if it's not already installed in your machine. If the structure of your xml is not fixed you can use the find_node function http://metacpan.org/pod/XML::Bare. (I have perl v5.10.1 installed)

Hope this helps

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.