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