Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP和SimpleXML解析ASMX响应_Php_Xml_Web Services - Fatal编程技术网

用PHP和SimpleXML解析ASMX响应

用PHP和SimpleXML解析ASMX响应,php,xml,web-services,Php,Xml,Web Services,我正在查询ASMX服务的值。该服务返回以下XML对象: <?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">

我正在查询ASMX服务的值。该服务返回以下XML对象:

<?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>
        <PerformSupportRequestResponse xmlns="http://www.example.com/API">
            <PerformSupportRequestResult>
                <Table>
                    <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:MainDataTable="Results" msdata:UseCurrentLocale="true">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="Results">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="TotalUsers" type="xs:long" 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">
                        <DocumentElement xmlns="">
                            <Results diffgr:id="Results1" msdata:rowOrder="0">
                                <TotalUsers>2494</TotalUsers>
                            </Results>
                        </DocumentElement>
                    </diffgr:diffgram>
                </Table>
            </PerformSupportRequestResult>
        </PerformSupportRequestResponse>
    </soap:Body>
</soap:Envelope>
空手而来。有没有一种简单的方法可以在不重新写入整个XML对象的情况下检索结果节点的成员?

一种简单的方法是使用

例子: 输出:
或者更简单,但基本上与SimpleXML的$elememt->xpath方法相同,它保存了创建DOMXPath对象的样板文件。
$media = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->PerformSupportRequestResponse->PerformSupportRequestResult->Table->children('urn:schemas-microsoft-com:xml-diffgram-v1')->diffgr->DocumentElement->Results
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

foreach ($xpath->query('//Results/*') as $result) {
    echo "$result->nodeName: $result->nodeValue";
}
TotalUsers: 2494