Home > linux, unix, xml > extract xml from the command line

extract xml from the command line

I just discovered this morning this cool utility in my /bin directory : xmllint

You can use it to extract values from your xml files within your shell scripts


$ cat foo.xml
<emplist>
  <emp no="1">
    <ename>John</ename>
  </emp>
  <emp no="2">
    <ename>Jack</ename>
  </emp>
</emplist>
$ echo 'cat //emplist/emp[@no="1"]/ename/text()'|
  xmllint --shell foo.xml |
  sed -n 3p
John

I like this !

Bookmark and Share

  1. egravers
    July 8th, 2010 at 03:11 | #1

    sed -n 3p
    did not work for me
    sed -n 2p
    did work!

    …or use sed to delete the lines with the “/ > ” prompt?

    sed ‘/^\/ >/ d’

  2. July 8th, 2010 at 10:45 | #2

    Ok, you have got a different input, thanks for your feedback

  1. No trackbacks yet.