Jump to content

Need to loop through XML file for each record


n1concepts

Recommended Posts

I have a php script defined that parsed the xml file and runs through the 1st record - encased within the EOF tags.

However, I need to capture ALL records - each defined between "EOF" tags - of the select xml file that will be pass to the script.

 

Currently, the php script I have finds the 1st set of info from the xml file but doesn't loop through the remaining records which all encased between the "EOF" tags. I know I need a while loop but not sure on the syntax to get it through the remaining files. Any comments appreciated to help edit code to form the required loop:

 

$xml = simplexml_load_file($new_location);
        foreach($xml->EOF as $eof) {
             $autoNoT  = $eof->AutoNo;
             $vegregnoT = $eof->TruckNo; 
             $clientIDT  = $eof->ClientID;
}

 

Here's the example xml file which contains multiple records that I need to extract each set of date between the <EOF></EOF> tags.

Note: the above php code extract just the 1st record then stops (I know I'm missing the loop but need help defining it...)

 

- <DocumentElement>
- <EOF>
  <AutoNo>109531</AutoNo> 
  <TruckNo>XLX885GP</TruckNo> 
  <ClientID>3</ClientID> 
  </EOF>
- <EOF>
  <AutoNo>109532</AutoNo> 
  <TruckNo>ZFN726GP</TruckNo> 
  <ClientID>3</ClientID> 
  </EOF>
- <EOF>
  <AutoNo>109533</AutoNo> 
  <TruckNo>ZFP113GP</TruckNo> 
  <ClientID>3</ClientID> 
  </EOF>
- <EOF>
  <AutoNo>109534</AutoNo> 
  <TruckNo>WSG299GP</TruckNo> 
  <ClientID>3</ClientID> 
  </EOF>
- <EOF>
  <AutoNo>109872</AutoNo> 
  <TruckNo>XKR493GP</TruckNo> 
  <ClientID>3</ClientID> 
  </EOF>
</DocumentElement>

Ok, I found some info on www.php.net that stated the problem is with the xml parser (makes FOREACH inop).

The suggestion was to use for loop and count number of nodes "EOF".

 

To that, I got the count working but yet to get array variable set for each instance per node (see code):

 

    $xml = simplexml_load_file($new_location);
$p_cnt = count($xml->EOF);
echo "<strong>THE TOTAL NUMBER OF NODES IN THIS FILE ARE: ".$p_cnt."<br />";
for($i = 0; $i < $p_cnt; $i++) {
  $param = $xml->EOF[$i];
  echo "The Node Info is: ".$param."<br />";
}

 

I just need to figure the $param portion to echo back each line within the <EOF></EOF> tags.

Any help appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.