Search code examples
xmlbashxmlstarletxmllint

SOAP XML parsing from shell


I need to get certain data from large xml file (in 2018 doh),it's an output from CMDBuild SOAP API request(old version, no REST support). I prefer using shell tools like xmlstarlet or xmllint. Here is XML:

<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns2:getCardListResponse
        xmlns:ns2="http://soap.services.cmdbuild.org">
        <ns2:return>
            <ns2:cards>
                <ns2:attributeList>
                    <ns2:name>instance_id</ns2:name>
                    <ns2:value>5919</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Description</ns2:name>
                    <ns2:value>CentOS-7-x86-64-1708</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>instance_name</ns2:name>
                    <ns2:value>i-2-5919-VM</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>hypervisor_hostname</ns2:name>
                    <ns2:value>ct-hn-v-4754875487</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_mac_address</ns2:name>
                    <ns2:value>1e:00:0f:00:04:97</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>base_hostname</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>cloudstack_name</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_ip_address</ns2:name>
                    <ns2:value>10.0.215.48</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Code</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>uuid</ns2:name>
                    <ns2:value>d54d08d0-83ef-47bb-b4aa-f0b2c8105639</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Notes</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:beginDate>2018-01-24T12:00:00.031+02:00</ns2:beginDate>
                <ns2:className>cloudstack</ns2:className>
                <ns2:id>1540303</ns2:id>
            </ns2:cards>
            <ns2:cards>
                <ns2:attributeList>
                    <ns2:name>instance_id</ns2:name>
                    <ns2:value>7259</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Description</ns2:name>
                    <ns2:value>CSCM-WLP-INTL-20GB</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>instance_name</ns2:name>
                    <ns2:value>i-92-7259-VM</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>hypervisor_hostname</ns2:name>
                    <ns2:value>ct-hn-v-85847487584</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_mac_address</ns2:name>
                    <ns2:value>1e:00:1e:00:04:7a</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>base_hostname</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>cloudstack_name</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>private_ip_address</ns2:name>
                    <ns2:value>10.0.215.19</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Code</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>uuid</ns2:name>
                    <ns2:value>eab5f52b-7225-4c51-9ca7-f26757a0d7f5</ns2:value>
                </ns2:attributeList>
                <ns2:attributeList>
                    <ns2:name>Notes</ns2:name>
                    <ns2:value></ns2:value>
                </ns2:attributeList>
                <ns2:beginDate>2018-01-24T16:15:00.018+02:00</ns2:beginDate>
                <ns2:className>cloudstack</ns2:className>
                <ns2:id>1542507</ns2:id>
            </ns2:cards>
            <ns2:totalRows>29</ns2:totalRows>
        </ns2:return>
    </ns2:getCardListResponse>
</soap:Body>

There is not much to add, how can I get for example all instane_name fields value, or private_ip_address ?

Br, Igor.


Solution

  • xmlstarlet solution:

    -- to select all "instance_name" nodes values:

    xmlstarlet sel -N soap="http://schemas.xmlsoap.org/soap/envelope/" \
    -N ns2="http://soap.services.cmdbuild.org" --net \
    -t -v "//ns2:attributeList/ns2:name[.='instance_name']/following-sibling::ns2:value" \
    -n input.xml
    

    The output:

    i-2-5919-VM
    i-92-7259-VM
    

    -- to select all "private_ip_address" nodes values:

    xmlstarlet sel -N soap="http://schemas.xmlsoap.org/soap/envelope/" \
    -N ns2="http://soap.services.cmdbuild.org" --net \
    -t -v "//ns2:attributeList/ns2:name[.='private_ip_address']/following-sibling::ns2:value" \
    -n input.xml
    

    The output:

    10.0.215.48
    10.0.215.19