DewBoy3d
DewBoy3d

Reputation: 163

Why wont simpleXML convert this xml to something usable?

I am attempting to consume a webservice in PHP. I have no control over the output of the webservice. It gives me what looks like good XML but simpleXML just returns an empty object back to me. Here is the XML that is returned from the webservice

<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://Service">
  <xs:schema id="WorkOrders" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="WorkOrders" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="WorkOrder">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="WorkOrder_x0023_" type="xs:string" minOccurs="0" />
                <xs:element name="DateEntered" type="xs:string" minOccurs="0" />
                <xs:element name="DateCompleted" type="xs:string" minOccurs="0" />
                <xs:element name="DateChanged" type="xs:string" minOccurs="0" />
                <xs:element name="Status" type="xs:string" minOccurs="0" />
                <xs:element name="Type" type="xs:string" 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">
    <WorkOrders xmlns="">
      <WorkOrder diffgr:id="WorkOrder1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
        <WorkOrder_x0023_>7164724</WorkOrder_x0023_>
        <DateEntered>07/19/11</DateEntered>
        <DateCompleted>07/21/11</DateCompleted>
        <DateChanged>07/21/11</DateChanged>
        <Status>CP</Status>
        <Type>IN</Type>
      </WorkOrder>
    </WorkOrders>
  </diffgr:diffgram>
</DataSet>

This is being obtained by a simple $xml = file_get_contents($url); call in my script. simplexml_load_string($xml); returns an empty object.

(object) SimpleXMLElement Object
(
)

I know I'm missing something here but I just cant put my finger on it. Any ideas?

Upvotes: 2

Views: 2191

Answers (1)

Jonathan Amend
Jonathan Amend

Reputation: 12815

You need to specify the namespace when accessing namespaced children and attributes like diffgram (and the WorkOrder's id). Try:

var_dump($xml->children('urn:schemas-microsoft-com:xml-diffgram-v1')->children());

(those child elements are back in the default namespace)

Reference: http://blog.sherifmansour.com/?p=302

Upvotes: 4

Related Questions