/dev/null

Extract a specific child element from XML file

In case you have an XML file structure based on strings (key = value), use this sed one liner to extract a specific child element from an XML file.


<Property>
        <Key>foobar</Key>
        <Value>The terms foobar (/ˈfuːbɑːr/), or foo and others are used as placeholder names (also referred to as metasyntactic variables) in computer programming or computer-related documentation.[1]</Value>
</Property>

Example:

sed -n 's|[ \t]*<Key>\(.*\)</Key>|\1|p' Source.xml > Target.txt

This will read what is inside every Key element in the source XML file and write the output to a text file named Target.txt. Replace Key with Value to extract values and so on.

First published on May 12, 2016