ScriptWriter
ScriptWriter

Reputation: 9

shell script to extract particular tag info from xml file

I want to extract <InFrame> tag information data for all respective <SOUNDCHEK> tags

Data from all <Inframe> tags in file is : -1 0 2 4 7

But I want <Inframe> info from <SOUNDCHEK> tag only : 2 4 7 [Desired output]

<APP>
    <Name>MainCheck</Name>
    <ActiveField>True</ActiveField>
    <InFrame>-1</InFrame>
    <CAMRECORD>
        <Name>Camera01</Name>
        <ActiveField>True</ActiveField>
        <InFrame>0</InFrame>
    </CAMRECORD>
    <SOUNDCHEK>
        <Name>Sound1.wav</Name>
        <ActiveField>True</ActiveField>
        <InFrame>2</InFrame>
    </SOUNDCHEK>
    <SOUNDCHEK>
        <Name>Sound2.wav</Name>
        <ActiveField>True</ActiveField>
        <InFrame>4</InFrame>
    </SOUNDCHEK>
    <SOUNDCHEK>
        <Name>Sound3.wav</Name>
        <ActiveField>True</ActiveField>
        <InFrame>7</InFrame>
    </SOUNDCHEK>
</APP>

Upvotes: 1

Views: 1313

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With xmlstalet tool:

xmlstarlet sel -t -v "//SOUNDCHEK/InFrame" -n input.xml
  • //SOUNDCHEK/InFrame - xpath expression to select all InFrame nodes which are children of SOUNDCHEK nodes

The output:

2
4
7

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416

Upvotes: 1

lher
lher

Reputation: 126

Try using xmllint and select the required data with a xpath.

xmllint --xpath '/APP/SOUNDCHECK/InFrame/text()' file.xml

Upvotes: 0

Related Questions