Skip to content
Advertisement

PHP Iterate Multiple Children from XML

I’m trying to read an XML file in PHP which has multiple same-named nodes inside a parent. So far, I can read the first iteration of the XML node but then PHP moves to the next parent even though there’s another child to be read. In this case, it’s the <DEVICE> node and the child node of it which is <IPV4>.

Here’s the XML (pass1.xml)

<LIST>
 <COMPANY>Alpha
  <DEVICE>Alpha1
   <IPV4>1.2.3.4</IPV4>
  </DEVICE>
  <DEVICE>Alpha2
   <IPV4>5.6.7.8</IPV4>
  </DEVICE>
 </COMPANY>
 <COMPANY>Bravo
  <DEVICE>Bravo1
   <IPV4>1.3.5.7</IPV4>
  </DEVICE>
 </COMPANY>
</LIST>

As you can see, there are two devices listed under the “Alpha” company and one under the “Bravo” company. Here’s my PHP (I’ve omitted the HTML tags):

        $xml = simplexml_load_file("/var/www/xml/pass1.xml") or die ("Could not open file!");

        // Get Companies
        echo "Company List<br>";
        foreach($xml->children() as $company) {
                echo $company . "<br>";
        }

        echo "<br>Device List<br>";

        // Get devices
        foreach($xml->children() as $device) {
                echo $device->DEVICE . "<br>";
        }

    echo "<br>IP List<br>";

    // Get IP
    foreach($xml->children() as $ipv4) {
            echo $ipv4->DEVICE->IPV4 . "<br>";
    }

The “Companies” are pulled properly but for the “Devices” I only get Alpha1 and Bravo1. Similarly, for the IP addresses I only get 1.2.3.4 and 1.3.5.7 which correspond with the devices which get pulled. I’m missing any information on Alpha2.

My ultimate goal is to have each Device and IPv4 iterated over from PHP and displayed. Can someone point me in the right direction?

Advertisement

Answer

I’m sure there’s a better way to do this, but this code explains why yours wasn’t working correctly.

    // Get Companies
    echo "Company List<br>";
    foreach($xml->children() as $company) {
            echo $company . "<br>";
    }

    echo "<br>Device List<br>";

    // Get devices
    foreach($xml->children() as $company) {
        foreach ($company->children() as $device) {
            echo $device . "<br>";
        }
    }

    echo "<br>IP List<br>";

    // Get IP
    foreach($xml->children() as $company) {
            foreach ($company->children() as $device) {
                foreach ($device->children() as $IPV4) {
                    echo $IPV4 . "<br>";
                }
            }
    }

The problem with your code is that in each loop you’re trying to pull different information, but your code is actually only pulling the first child repeatedly.

So here

   // Get devices
    foreach($xml->children() as $device) {
            echo $device->DEVICE . "<br>";
    }

You’re attempting to get each DEVICE from the XML file, but in actuality you’re pulling all of the FIRST children from $xml which are COMPANY tags, and the two FIRST COMPANY tags are Alpha and Bravo. and then with EACH of those two company tags you’re echoing a SINGLE DEVICE tag.

So you’re pulling a SINGLE DEVICE tag from Alpha and a SINGLE DEVICE tag from Bravo. So the solution was to add another layer to your foreach, where after it loops through the two COMPANY tags, it then loops through the next layer of children of the COMPANY tags, which are the DEVICE tags.

Then for the IPV4 you do the same, but add another loop layer.

2 People found this is helpful
Advertisement