(It has been suggested this question has already been answered here: PHP convert XML to JSON. But as detailed below I have already used simplexml_load_string which has not been successful).
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:
These are the methods I have tried:
$xml = \Zend\Xml2Json\Xml2Json::fromXml($data, false);
$doc = new \DOMDocument();
$doc->loadXML($data);
$x = $doc->documentElement;
$data = simplexml_load_string($data);
The result from the methods above:
{"Success":"true","FailMessage":{},"Returned_DataSet":{}}
I am looking to get the data from the result:
code: 47156
name: BARB
code: 1
name: GREATN
code: 89252
name: DERIN
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<DataSetResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://domain.co.uk/vehicles">
<Success>true</Success>
<FailMessage />
<Returned_DataSet>
<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:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="CMan_Code" type="xs:int" minOccurs="0" />
<xs:element name="CMan_Name" 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">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<CMan_Code>47156</CMan_Code>
<CMan_Name>BARB </CMan_Name>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<CMan_Code>1</CMan_Code>
<CMan_Name>GREATN </CMan_Name>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<CMan_Code>89252</CMan_Code>
<CMan_Name>DERIN </CMan_Name>
</Table>
</NewDataSet>
</diffgr:diffgram>
</Returned_DataSet>
</DataSetResult>