parsing SOAP XML response with php

2019-02-20 06:00发布

问题:

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 can consider me a beginner.

Here is a sample of the response I get

<DataSet xmlns="http://www.multiprets.net/api">
    <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="Table">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="ProductSequence" type="xs:string" minOccurs="0"/>
                                <xs:element name="ProductCode" type="xs:string" minOccurs="0"/>
                                <xs:element name="ProdNameEn" type="xs:string" minOccurs="0"/>
                                <xs:element name="ProdNameFr" type="xs:string" minOccurs="0"/>
                                <xs:element name="ProductTerm" type="xs:double" minOccurs="0"/>
                                <xs:element name="UpdDate" type="xs:int" minOccurs="0"/>
                                <xs:element name="UpdTime" type="xs:int" minOccurs="0"/>
                                <xs:element name="Posted" type="xs:double" minOccurs="0"/>
                                <xs:element name="MPH" type="xs:double" minOccurs="0"/>
                                <xs:element name="CNT" type="xs:int" minOccurs="0"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
        <NewDataSet xmlns="">
            <Table diffgr:id="Table1" msdata:rowOrder="0">
                <ProductSequence>A1999</ProductSequence>
                <ProductCode>VC</ProductCode>
                <ProdNameEn>3 years variable</ProdNameEn>
                <ProdNameFr>3 ans variable</ProdNameFr>
                <ProductTerm>3</ProductTerm>
                <UpdDate>20140529</UpdDate>
                <UpdTime>134900</UpdTime>
                <Posted>3</Posted>
                <MPH>3</MPH>
                <CNT>1</CNT>
            </Table>
            <Table diffgr:id="Table2" msdata:rowOrder="1">
                <ProductSequence>A4000</ProductSequence>
                <ProductCode>VC</ProductCode>
                <ProdNameEn>5 years variable</ProdNameEn>
                <ProdNameFr>5 ans variable</ProdNameFr>
                <ProductTerm>5</ProductTerm>
                <UpdDate>20141021</UpdDate>
                <UpdTime>103100</UpdTime>
                <Posted>3</Posted>
                <MPH>2.3</MPH>
                <CNT>1</CNT>
            </Table>

I have tried to get the <MPH> tag from table8 using this code:

$client = new soapclient("http://www.multiprets.net/api/ws/services.asmx?wsdl"); 
$objectresult = $client->Recupere_meilleurs_Taux(array('Code_businessID'=> '**LoginString**','trace' => 1)); 
$resultat = simplexml_load_string($objectresult->Recupere_meilleurs_TauxResult->any); 
foreach ($resultat->xpath('//Table[@id='Table8']') as $product){ echo $product->MPH."< br/ >"; }

To no avail as it's not returning any result. If I remove the attribute from the xpath and only keep //Table I get the MPH from all the table, so I am sure it's something with the attribute @id

Thank you in advance for your time and for sharing your knowledge.

回答1:

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:

<diffgr:diffgram 
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
    xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">

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


回答2:

retrieve value from this type response 



<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <xyzResponse xmlns="http://tempuri.org/">
                <xyzResult>
                    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="Table">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="myCityID" type="xs:int" minOccurs="0" />
                                                <xs:element name="myCityName" type="xs:string" minOccurs="0" />

                                                <xs:element name="myLat" type="xs:double" minOccurs="0" />
                                                <xs:element name="myLon" type="xs:double" minOccurs="0" />
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:choice>
                            </xs:complexType>
                        </xs:element>
                    </xs:schema>
                    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                        <NewDataSet xmlns="">
                            <Table diffgr:id="Table1" msdata:rowOrder="0">
                                <CityID>1</CityID>
                                <CityName>Ahmedabad</CityName>
                                <Lat>23.045839</Lat>
                                <Lon>72.550578</Lon>
                            </Table>
                            <Table diffgr:id="Table2" msdata:rowOrder="1">
                                <CityID>21</CityID>
                                <CityName>Amritsar</CityName>
                                <Lat>31.705603</Lat>
                                <Lon>74.807337</Lon>
                            </Table>
                        </NewDataSet>
                    </diffgr:diffgram>
                </xyzResult>
            </xyzResponse>
        </soap:Body>
    </soap:Envelope>

using this code you can easily get all the Tables data in $title variable and using that you can easily get any specific value easily...

                $sxe = new SimpleXMLElement($response);
                $sxe->registerXPathNamespace('d', 'urn:schemas-microsoft-com:xml-diffgram-v1');
                $result = $sxe->xpath("//NewDataSet");
                echo "<pre>";
                //print_r($result[0]);
                foreach ($result[0] as $title) {
                    print_r($title);

                }