0

So I'm connecting to an api and it is returning XML.

$vehicles = array();

$xml = simplexml_load_string($result);
foreach($xml->Result as $item)
{
   $vehicle = array();

   foreach($item as $key => $value)
   {
        $vehicle[(string)$key] = (string)$value;
   }

   $vehicles[] = $vehicle;
}

print_r($vehicles);

If I var_dump($result) it displays (in the source code):

 string(52055) "<?xml version="1.0" encoding="utf-8"?>
<DataTable xmlns="http://tempuri.org/">
  <xs:schema id="DataSet" 
targetNamespace="http://tempuri.org/DataSet.xsd" 
xmlns:mstns="http://tempuri.org/DataSet.xsd" 
xmlns="http://tempuri.org/DataSet.xsd" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="DataSet" msdata:IsDataSet="true" msdata:MainDataTable="http_x003A__x002F__x002F_tempuri.org_x002F_DataSet.xsd_x003A_Result" msdata:UseCurrentLocale="true">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Result">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="newUsed" type="xs:string" minOccurs="0" />
            <xs:element name="unitNumber" type="xs:string" minOccurs="0" />
            <xs:element name="SN" type="xs:string" minOccurs="0" />
            <xs:element name="Make" type="xs:string" minOccurs="0" />
            <xs:element name="Model" type="xs:string" minOccurs="0" />
            <xs:element name="mYear" type="xs:string" minOccurs="0" />
            <xs:element name="availability" 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">
<DataSet xmlns="http://tempuri.org/DataSet.xsd">
  <Result diffgr:id="Result1" msdata:rowOrder="0">
    <newUsed>Used</newUsed>
    <unitNumber>3621P</unitNumber>
    <SN>1M2AG11C94M013645</SN>
    <Make>Mack</Make>
    <Model>CV713</Model>
    <mYear>2004</mYear>
    <availability>In Stock</availability>
      </Result>
    </DataSet>
  </diffgr:diffgram>
</DataTable>"

My question is, why is this returning an empty array? What am I missing? NOTE: I've removed a lot of the xml for easier reading.

0

2 Answers 2

1

You're running into problems due to the namespaces in the XML document (the elements with : in them). You can't access child elements in different namespaces using the standard -> SimpleXML operator - you need to either loop over them using the children() method, or use Xpath to access them. I prefer the latter, although depending on the structure of your document that might not be possible.

$xml = simplexml_load_string($result);
$xml->registerXPathNamespace("DataSet", "http://tempuri.org/DataSet.xsd");

foreach($xml->xpath('//DataSet:Result') as $item) {
  $vehicle = array();

  foreach($item->children() as $value) {
    $vehicle[$value->getName()] = (string) $value;
  }

  $vehicles[] = $vehicle;
}

print_r($vehicles);

I've also made a change to how you were accessing the children of the <Result> elements - you can't use a foreach loop to access elements as key/value pairs. I've replaced this with a call to getName for the tag name, and cast to a string for the element's content.

See https://eval.in/864475 for a full example.

Sign up to request clarification or add additional context in comments.

Comments

1

$xml is the root element of the xml, so its DataTable. Between DataTable and Result there is a node in the hierarchy called DataSet.

So you code should be

foreach($xml->DataSet->Result as $item)
{
   ...
}

This is an example, you must test if $xml->DataSet exists before accessing his children.

1 Comment

There is also diffgr:diffgram between DataTable and DataSet in the element structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.