0

I am stuck here with this problem. I have an XML file that LOOKS LIKE THIS:

<?xml version="1.0" encoding="UTF-8"?>  
<ListVehicleInventory xmlns="http://www.starstandards.org/STAR"  
xmlns:oa="http://www.openapplications.org/oagis" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.starstandards.org/STAR/STAR/Rev1.1/BODs/ListVehicleInventory.xsd" revision="1.0" release="8.1" environment="Production" lang="en-US">
<ApplicationArea>
    <Sender>
      <Component>XPO</Component>
      <Task>VehicleInventory</Task>
      <CreatorNameCode>AUTO123</CreatorNameCode>
      <SenderNameCode>XPO</SenderNameCode>
      <Language>eng</Language>
    </Sender>
    <CreationDateTime>2013-04-26T00:16:49-0400</CreationDateTime>
    <BODId>2013042601</BODId>
    <Destination>
      <DestinationNameCode>CBB</DestinationNameCode>
    </Destination>
  </ApplicationArea>
  <DataArea>
    <VehicleInventory>
      <Header>
        <TransactionType>Delete</TransactionType>
      </Header>
      <Invoice>
        <Vehicle>
          <ModelYear>2011</ModelYear>
          <Make>Dodge</Make>

...

DataArea as all the vehicle Inventory information that I want to pull out. I am trying to get a foreach going but it only find 1 VehicleInventory attributes and not going thru the foreach to all the data in the file. I have 3 vehicleInventory data in my XML for this testing.

Here is my actual coding:

$xml = simplexml_load_file($xmlDir . DIRECTORY_SEPARATOR . 'test.xml');  
foreach($xml as $vehicle):  
echo '<pre>';  
print_r($vehicle->VehicleInventory->Invoice->Vehicle);  
echo '</pre>';  
endforeach;

Result: THis shows me all the info for the 1st VehicleINventory but doens't loop. What is wrong?

1

1 Answer 1

1

The loop should be:

foreach($xml->DataArea->VehicleInventory as $vehicle)
{
    print_r($vehicle->Invoice->Vehicle);
}

Your original code is looping over $xml. The problem with that is SimpleXML puts the root node in $xml, so in your case $xml is <ListVehicleInventory>.

Codepad Demo

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

4 Comments

OK, this helps a bit, but I can't seems to loop anyway. DataArea only comes 1 time in my XML, if I use it, I won'T loop more than once?
That's not a problem, have a look at the demo. This code loops over the VehicleInventory. As you can see, the demo outputs Dodge, Ford, Aston Martin.
What do you mean by look at the demo?
My eyes are playing tricks with me, I couldn'T see your link, as big as it is... Sorry.

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.