Simplest way to do basic xml parsing from unix command line Simplest way to do basic xml parsing from unix command line xml xml

Simplest way to do basic xml parsing from unix command line


The following linux command uses XPath to access specified values within the XML file

for xml in `find . -name "*.xml"`do  echo $xml `xmllint --xpath "/param-value/value/text()" $xml`| awk 'NF>1'done

Example output for matching XML files:

./test1.xml asdf./test4.xml 1234


I worked out a couple of solutions using basic perl/awk functionality (basically a poor man's parsing of the tags). If you see any improvements using only basic perl/awk functionality, let me know. I avoided dealing with multiline regular expressions by setting a flag with I see a particular tag. Kind of clumsy but it works.

perl:

perl -ne '$h = 1 if m/Host/; $r = 1 if m/Role/; if ($h && m/<value>/) { $h = 0; print "hosts: ", $_ =~ /<value>(.*)</, "\n"}; if ($r && m/<value>/) { $r = 0; print "\nrole: ", $_ =~ /<value>(.*)</, "\n" }'

awk:

awk '/Host/ {h = 1} /Role/ {r = 1} h && /<value>/ {h = 0; match($0, "<value>(.*)<", a); print "hosts: " a[1]} r && /<value>/ {r = 0; match($0, "<value>(.*)<", a); print "\nrole: " a[1]}'


$ xmlstarlet ed -u /param-value/name -v Roles -u /param-value/value -v asdf data.xml<?xml version="1.0"?><param-value>  <name>Roles</name>  <description>some description</description>  <value>asdf</value></param-value>