Santhosh Fernando
Santhosh Fernando

Reputation: 137

Get a value from xml on fly in shell script

I got a xml in a location 'http://10.xxx.xxx.xx:8080/mbci/collection1/dataimport?command=status' which have the xml data in the format.

<response>
    <lst name="responseHeader">
        <int name="status">
            0
        </int>
        <int name="QTime">
            0
        </int>
    </lst>
    <lst name="initArgs">
        <lst name="defaults">
            <str name="config">
                data-config.xml
            </str>
        </lst>
    </lst>
    <str name="command">
        status
    </str>
    <str name="status">
        idle
    </str>
    <str name="importResponse">
        A command is still running...
    </str>
    <lst name="statusMessages">
        <str name="Time Elapsed">
            0:2:13.132
        </str>
        <str name="Total Requests made to DataSource">
            6
        </str>
        <str name="Total Rows Fetched">
            755949
        </str>
        <str name="Total Documents Skipped">
            0
        </str>
        <str name="Full Dump Started">
            2014-11-18 04:00:18
        </str>
    </lst>
    <str name="WARNING">
        This response format is experimental. It is likely to change in the future.
    </str>
</response>

I should get the '' value from it. i cant able to find a direct way to do it so i dowload the file and then parse the xml using the following script

wget http://10.xxx.xxx.xx:8080/mbci/collection1/dataimport?command=status -O status.xml
status=$(grep -oP '(?<="status">).*(?=</str)' status.xml)

But i need to know is there any other easier way to do it rather than download and parse

Upvotes: 0

Views: 94

Answers (1)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185760

are definitely not the good tool to query an document. Instead, use a proper xml parser.

In , there's 2 good tools for this kind of task, xmllint and xmlstarlet, you will be able to query the document using expressions. This is the way to go !

Example :

$ xmllint --xpath '//str[@name="importResponse"]/text()' file

    A command is still running...

Upvotes: 1

Related Questions