本文介绍了用 php 解析 SOAP XML 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了多个示例和 w3Schools 教程,但我无法弄清楚 SOAP 响应的结构.我已经 10 多年没有接触过 php/xml,所以你可以认为我是初学者.

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.

这是我得到的回复样本

<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>

我尝试使用以下代码从 table8 获取 标记:

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/ >"; }

无济于事,因为它没有返回任何结果.如果我从 xpath 中删除属性并只保留 //Table 我会从所有表中获得 MPH,所以我确定它与属性 @id

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.

推荐答案

这里的问题是你的属性有一个命名空间,所以你需要用 SimpleXML XPath 注册 ns 并在你的 XPath 查询中使用它.

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">

要使用 SimpleXML 注册这些命名空间,您需要使用函数 registerXPathNamespace.根据您正在执行的其他查询,您可能想要注册两个命名空间.对于您的查询,您需要 urn:schemas-microsoft-com:xml-diffgram-v1 (diffgr) 命名空间;像这样注册:

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');

现在您可以访问 diffgr 命名空间中的任何元素或属性,方法是在其前面加上字母 d.下面是一个使用 Table 元素和 id Table1 的例子:

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;
}

输出:

A1999

这篇关于用 php 解析 SOAP XML 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:45