简体   繁体   English

在Shell脚本中即时从xml获取值

[英]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. 我在位置“ http://10.xxx.xxx.xx:8080 / mbci / collection1 / dataimport?command = status ”中获得了一个xml格式的XML数据。

<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 我无法找到一种直接的方式来做到这一点,所以我下载了文件,然后使用以下脚本解析了xml

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 但是我需要知道还有其他比下载和解析更简单的方法吗?

are definitely not the good tool to query an document. 绝对不是查询文档的好工具。 Instead, use a proper xml parser . 相反,请使用适当的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. ,有2个出色的工具xmllintxmlstarlet可以完成此类任务,您将能够使用表达式查询文档。 This is the way to go ! 这是要走的路 !

Example : 范例:

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

    A command is still running...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM