Bash/Parse XML

Aus SchnallIchNet
Wechseln zu: Navigation, Suche

Parse XML in BASH...

simple XMP parsing

to parse really simple xml like:

<tag>
 <tag1>VALUE</tag1>
</tag>

i wrote a function and placed it in my .bashrc...

# simple XML/HTML parse function
# used to extrace values from a really simple result.xml file.
# 
# use like this:
# while rdom; do
#  if [[ $TAG = "TAG-Name-Im-Looking-For" ]]; then
#     echo $VAL;
#  fi;
# done < /home/user/results.xml
#
rdom() {
   export TAG; export VAL;
   local IFS=\> ;
   read -d \< TAG VAL ;
}


more complex XML files

for more complex xml-files prefer to use perl!

install cpan-module: XML::XPath

cpan install XML::XPath

this places a binary on you system:

/usr/bin/xpath

this can be used to extract values by XPath...

xpath /home/user/results.xml '/startNode/childNode1[5]/@attribute'

by echoing a single line and get a single attribute i query like this:

echo '<tag att1="a1val" att2="a2val" att3="a3val" [... attX="aXval"] />' | xpath '//@att2'