Hi,
This is the first time I have had to consume XML returned from a webservice and I am having a bit of trouble and was hoping somebody would be able to help.
I can make the call ok to the web service ok using the following :
$rawFeed = file_get_contents($url);
If I do a echo $xml at this stage it outputs all returned data minus the element tags. So I then do:
$xml = new SimpleXmlElement($rawFeed);
But this is where I get confused on how to access each of the the XML nodes to populate the array.
The file that is returned as follows:
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://localhost/WSBatteries2/WS">
<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">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="StdData">
<xs:complexType>
<xs:sequence>
<xs:element name="MAKE" type="xs:string" minOccurs="0" />
<xs:element name="MODEL" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="BatteryData">
<xs:complexType>
<xs:sequence>
<xs:element name="fldBatteryType" type="xs:string" minOccurs="0" />
<xs:element name="fldBatteryCategory" 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="">
<StdData diffgr:id="StdData1" msdata:rowOrder="0">
<MAKE>MINI</MAKE>
<MODEL>MINI COOPER S</MODEL>
</StdData>
<BatteryData diffgr:id="BatteryData1" msdata:rowOrder="0">
<fldBatteryType>065</fldBatteryType>
<fldBatteryCategory>Premium</fldBatteryCategory>
</BatteryData>
<BatteryData diffgr:id="BatteryData2" msdata:rowOrder="1">
<fldBatteryType>075</fldBatteryType>
<fldBatteryCategory>Calcium</fldBatteryCategory>
</BatteryData>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
I think I need to do something like the following:
$stdData['MAKE'] = $xml->stdData->MAKE;
But it does not work, could somebody please help me and tell me how to:
a) get one of the elements values by their name i.e. the MAKE in stdData
b) create an array that contained the 2 BatteryData sets so I would then be able to loop through them.
Thanks for your time
Richard