0

I'm receiving xml data from a api request that I'm finding difficult to parse. I've included and example of what the response data looks like vs how I need it to parse it. I've included the code I'm using to parse the XML. My question is it is not very user friendly to use $xml->xpath('//SOAP-ENV:Body/ns1:add_purchase_orderResponse/return/item/value/item/value') I would prefer to use $xml->xpath('//SOAP-ENV:Body/ns1:add_purchase_orderResponse/return/result/status/code')

I'm I doing something wrong, or I'm I just stuck with ugly code because I'm getting ugly XML.

<?php
$orderXML = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://qqp.mypresswise.com/r/wsdl-order.php" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns2="http://xml.apache.org/xml-soap" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:add_purchase_orderResponse>
            <return xsi:type="ns2:Map">
                <item>
                    <key xsi:type="xsd:string">result</key>
                    <value xsi:type="ns2:Map">
    <item>
        <key xsi:type="xsd:string">status</key>
        <value xsi:type="ns2:Map">
            <item>
                <key xsi:type="xsd:string">code</key>
                <value xsi:type="xsd:int">0</value>
            </item>
            <item>
                <key xsi:type="xsd:string">text</key>
                <value xsi:type="xsd:string">ok</value>
            </item>
        </value>
    </item>
                        <item>
                            <key xsi:type="xsd:string">data</key>
                            <value xsi:type="ns2:Map">
                                <item>
                                    <key xsi:type="xsd:string">webID</key>
                                    <value xsi:type="xsd:string">ZTJmYTA0NmI4NmVk</value>
                                </item>
                                <item>
                                    <key xsi:type="xsd:string">orderID</key>
                                    <value xsi:type="xsd:string">44604</value>
                                </item>
                                <item>
                                    <key xsi:type="xsd:string">items</key>
                                    <value xsi:type="ns2:Map">
                                        <item>
                                            <key xsi:type="xsd:string">item</key>
                                            <value SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
                                                <item xsi:type="ns2:Map">
                                                    <item>
                                                        <key xsi:type="xsd:string">itemID</key>
                                                        <value xsi:type="xsd:int">107263</value>
                                                    </item>
                                                    <item>
                                                        <key xsi:type="xsd:string">itemprice</key>
                                                        <value xsi:type="xsd:string">42</value>
                                                    </item>
                                                </item>
                                            </value>
                                        </item>
                                    </value>
                                </item>
                            </value>
                        </item>
                    </value>
                </item>
            </return>
        </ns1:add_purchase_orderResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;


$xml = simplexml_load_string($orderXML);

foreach($xml->xpath('//SOAP-ENV:Body/ns1:add_purchase_orderResponse/return/item/value/item/value') as $xmlItem) {



   /*if ($xmlItem->item[0]->key == "code"){
       echo "value=>" . $xmlItem->item[0]->value;
   } */

    if ($xmlItem->item[1]->key == "orderID"){
        echo "value=>" . $xmlItem->item[1]->value;
    }   
}
?>
  • 1
    XML is meant to be easily parse-able -- that's kind of the point of it. You shouldn't ever have to reformat it. Working with your source XML (as pedantic as it is) shouldn't be difficult as long as you use an XML parser. I like to use [SimpleXML](http://us3.php.net/manual/en/book.simplexml.php) for things like this. – Alex Howansky Jul 11 '18 at 16:42
  • You should show your attempt to parse the original and explain what difficulties you're having. Also, any reformatting would require parsing the original anyway (how else would you extract the needed info?), so you'd just be back at square one. – Patrick Q Jul 11 '18 at 16:46
  • I edited the original question to show my method of parsing the code, and further explaining my question. – Jerome Wild Jul 11 '18 at 20:50

0 Answers0