user3636706
user3636706

Reputation: 207

How to iterate over a xml file by xmlstarlet

suppose I have a xml file as below:

<root>
<element1>value1</element1>
<element2>value2</element2>
<element3>value3</element3>
</root>

I want to iterate over this xml file, and store the element name and value in a map. How to do that by xmlstarlet? Any help will be appreciated!

Upvotes: 1

Views: 1772

Answers (1)

Dustin Glover
Dustin Glover

Reputation: 176

this should do it

xmlstarlet sel -t -m "/root[*]" -v . your.xml

Explanation:

sel -t  

standard setup for select with output

-m "/root[*]"

This requires a match (-m) at the literal "root" of the document '/root' and for every direct node, in this case defined as an XPath wildcard (*), do something. (FYI - to get funky with xmlstarlet you have to use a little XPath - just make sure to always wrap in (). also handy when using '-i')

-v .

This is requesting the value of an element in the current path (-v) and the '.' is for the current element in the iteration. (since we don't know the name)

your.xml

file name. (yours, not mine)

Hope that helps you out.

-DG

Upvotes: 1

Related Questions