Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML using unix terminal

Sometimes I need to quickly extract some arbitrary data from XML files to put into a CSV format. What's your best practices for doing this in the Unix terminal? I would love some code examples, so for instance how can I get the following problem solved?

Example XML input:

<root>
    <myel name="Foo" />
    <myel name="Bar" />
</root>

My desired CSV output:

Foo,
Bar,
like image 483
Mattias Avatar asked Aug 26 '08 20:08

Mattias


2 Answers

Using xidel:

xidel -s input.xml -e '//myel/concat(@name,",")'
like image 94
Reino Avatar answered Oct 03 '22 22:10

Reino


XMLStarlet is a command line toolkit to query/edit/check/transform XML documents (for more information, see XMLStarlet Command Line XML Toolkit)

No files to write, just pipe your file to xmlstarlet and apply an xpath filter.

cat file.xml | xml sel -t -m 'xpathExpression' -v 'elemName' 'literal' -v 'elname' -n

-m expression -v value '' included literal -n newline

So for your xpath the xpath expression would be //myel/@name which would provide the two attribute values.

Very handy tool.

like image 31
DaveP Avatar answered Oct 03 '22 21:10

DaveP