title: PHP complex XML to JSON parsing
body: <p>(It has been suggested this question has already been answered here: <a href="https://stackoverflow.com/questions/8830599/php-convert-xml-to-json">PHP convert XML to JSON</a>. But as detailed below I have already used simplexml_load_string which has not been successful). </p>
  
  <hr>
  
  <p>I have tried multiple ways to get the content on an XML file to JSON, but the XML file is complex and fails with every attempt:</p>
  
  <p>These are the methods I have tried:</p>
  
  <pre><code>$xml = \Zend\Xml2Json\Xml2Json::fromXml($data, false);
  
  $doc = new \DOMDocument();
  $doc-&gt;loadXML($data);
  $x = $doc-&gt;documentElement;
  
  $data = simplexml_load_string($data);
  </code></pre>
  
  <p>The result from the methods above:</p>
  
  <pre><code>{"Success":"true","FailMessage":{},"Returned_DataSet":{}}
  </code></pre>
  
  <p>I am looking to get the data from the result:</p>
  
  <pre><code>code: 47156
  name: BARB
  code: 1
  name: GREATN
  code: 89252
  name: DERIN  
  </code></pre>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;DataSetResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://domain.co.uk/vehicles"&gt;
    &lt;Success&gt;true&lt;/Success&gt;
    &lt;FailMessage /&gt;
    &lt;Returned_DataSet&gt;
      &lt;xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"&gt;
        &lt;xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
              &lt;xs:element name="Table"&gt;
                &lt;xs:complexType&gt;
                  &lt;xs:sequence&gt;
                    &lt;xs:element name="CMan_Code" type="xs:int" minOccurs="0" /&gt;
                    &lt;xs:element name="CMan_Name" type="xs:string" minOccurs="0" /&gt;
                  &lt;/xs:sequence&gt;
                &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
            &lt;/xs:choice&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:schema&gt;
      &lt;diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"&gt;
        &lt;NewDataSet xmlns=""&gt;
          &lt;Table diffgr:id="Table1" msdata:rowOrder="0"&gt;
            &lt;CMan_Code&gt;47156&lt;/CMan_Code&gt;
            &lt;CMan_Name&gt;BARB                   &lt;/CMan_Name&gt;
          &lt;/Table&gt;
          &lt;Table diffgr:id="Table2" msdata:rowOrder="1"&gt;
            &lt;CMan_Code&gt;1&lt;/CMan_Code&gt;
            &lt;CMan_Name&gt;GREATN               &lt;/CMan_Name&gt;
          &lt;/Table&gt;
          &lt;Table diffgr:id="Table3" msdata:rowOrder="2"&gt;
            &lt;CMan_Code&gt;89252&lt;/CMan_Code&gt;
            &lt;CMan_Name&gt;DERIN                   &lt;/CMan_Name&gt;
          &lt;/Table&gt;
        &lt;/NewDataSet&gt;
      &lt;/diffgr:diffgram&gt;
    &lt;/Returned_DataSet&gt;
  &lt;/DataSetResult&gt;
  </code></pre>