Saturday, 6 December 2014

Parsing ASMX response with PHP and SimpleXML



I'm querying an ASMX service for a value. The service returns the following XML object:



<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://ift.tt/sVJIaE" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<soap:Body>
<PerformSupportRequestResponse xmlns="http://ift.tt/1vXUAFs">
<PerformSupportRequestResult>
<Table>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://ift.tt/tphNwY" 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>


I've tried working with this using SimpleXML and even flirted with DOMDocument. I have to believe that there is an easy way to load the values that will be returned between <Results> and </Results> into an array, but so far the only reliable method has been to remove any line that contains a namespace or doesn't contain a result I want. I don't think this is practical because I have to make several calls to this service with differing queries, and the responses aren't always going to be the same, but they will always be in the Results node of the response.


I've also tried to breadcrumb my way to the answers, using something like:



$media = $xml->children('http://ift.tt/sVJIaE')->Body->PerformSupportRequestResponse->PerformSupportRequestResult->Table->children('urn:schemas-microsoft-com:xml-diffgram-v1')->diffgr->DocumentElement->Results


and have come up empty-handed. Is there an easy way to retrieve the members of the Results node without resorting to re-writing the entire XML object?


No comments:

Post a Comment