parsing SOAP XML response with php

前端 未结 2 1492
后悔当初
后悔当初 2021-01-06 23:13

I have checked multiple examples and the w3Schools tutorial but I can\'t figure out the structure of the SOAP response. I haven\'t touch php/xml in more than 10 years so you

2条回答
  •  抹茶落季
    2021-01-07 00:00

    The problem here is that your attribute has a namespace, so you need to register the ns with SimpleXML XPath and use it in your XPath query.

    This element declares two namespaces for its descendants:

    
    

    To register those namespaces with SimpleXML, you need to use the function registerXPathNamespace. Depending on what other queries you're doing, you might want to register both namespaces. For your query, you need the urn:schemas-microsoft-com:xml-diffgram-v1 (diffgr) namespace; register it like so:

    $sxe = new SimpleXMLElement($your_xml_here);
    $sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
    

    Now you can access any element or attribute in the diffgr namespace by prefixing it with the letter d. Here's an example using the Table element with id Table1:

    $tables = $sxe->xpath('//Table[@d:id="Table1"]');
    foreach ($tables as $t) {
        echo $t->ProductSequence . PHP_EOL;
    }
    

    Output:

    A1999
    

提交回复
热议问题