1

I retrieve the following XML data:

<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" MaxVersion="1.4" SenderID="HP Hybrid Elk JMF" TimeStamp="2014-02-19T07:42:11+00:00" Version="1.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="JMFRootMessage">
 <!--Generated by the CIP4 Java open source JDF Library version : CIP4 JDF Writer Java 1.4a BLD 63-->
 <Response ID="Rgdhhhdfhd" ReturnCode="0" Type="KnownDevices" refID="gdhhhdfhd" xsi:type="ResponseKnownDevices">
   <DeviceList>
     <DeviceInfo DeviceCondition="OK" DeviceID="HPSSPP-SM" DeviceStatus="Running" StatusDetails="Running"/>
     <DeviceInfo CounterUnit="Impressions" DeviceCondition="OK" DeviceID="192.168.1.101" DeviceStatus="Running" ProductionCounter="12345678" StatusDetails="Indigo: Printing" xmlns:jdf="http://www.CIP4.org/JDFSchema_1_1">
       <GeneralID IDUsage="hpCustomerImpressionCounter" IDValue="12345678.0"/>
     </DeviceInfo>
     <DeviceInfo CounterUnit="Impressions" DeviceCondition="OK" DeviceID="192.168.1.102" DeviceStatus="Running" ProductionCounter="23456789" StatusDetails="Indigo: Printing" xmlns:jdf="http://www.CIP4.org/JDFSchema_1_1">
       <GeneralID IDUsage="hpCustomerImpressionCounter" IDValue="23456789.0"/>
     </DeviceInfo>
   </DeviceList>
 </Response>
</JMF>

I load it into a SimpleXMLElement:

<?php
$jdf_response = new SimpleXMLElement($xml_response);

And I can then display it like so:

<pre>
<?php print_r($jdf_response->Response->DeviceList); ?>
</pre>

Which gives the following output:

SimpleXMLElement Object
(
    [DeviceInfo] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [DeviceCondition] => OK
                            [DeviceID] => HPSSPP-SM
                            [DeviceStatus] => Running
                            [StatusDetails] => Running
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [CounterUnit] => Impressions
                            [DeviceCondition] => OK
                            [DeviceID] => 192.168.1.101
                            [DeviceStatus] => Running
                            [ProductionCounter] => 12345678
                            [StatusDetails] => Indigo: Printing
                        )

                    [GeneralID] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [IDUsage] => hpCustomerImpressionCounter
                                    [IDValue] => 12345678.0
                                )

                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [CounterUnit] => Impressions
                            [DeviceCondition] => OK
                            [DeviceID] => 192.168.1.102
                            [DeviceStatus] => Running
                            [ProductionCounter] => 23456789
                            [StatusDetails] => Indigo: Printing
                        )

                    [GeneralID] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [IDUsage] => hpCustomerImpressionCounter
                                    [IDValue] => 23456789.0
                                )

                        )

                )

        )

)

So far so good. But I need to get the data from the DeviceInfo array, so I modify the code:

<pre>
<?php print_r($jdf_response->Response->DeviceList->DeviceInfo); ?>
</pre>

But instead of three SimpleXMLElement objects, I get only the first.

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [DeviceCondition] => OK
            [DeviceID] => HPSSPP-SM
            [DeviceStatus] => Running
            [StatusDetails] => Running
        )

)

What am I doing wrong?

Update: The reason I was using print_r() in the first place because because I was getting no output from the following code:

<?php

$addresses = array();

foreach ($jdf_response->Response->DeviceList->DeviceInfo as $device) {
    $addresses[] = $device->{'@attributes'}'DeviceID'];
}
print_r($addresses);
Kalessin
  • 2,282
  • 2
  • 22
  • 24

1 Answers1

1

Example #4 Accessing non-unique elements in SimpleXML

When multiple instances of an element exist as children of a single parent element, normal iteration techniques apply.

Data is still there, You just have to use an iterator like:

foreach($jdf_response->Response->DeviceList->DeviceInfo as $device)
{
 print_r($device);
}

Reference

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Thanks. I get why that would work, but not why `print_r()` doesn't. I also don't understand why `print_r($device->{'@attributes'})` doesn't work inside the foreach loop. I'll update my question to reflect this. – Kalessin Feb 19 '14 at 09:51
  • 1
    This might also help you in understanding this phenomena http://stackoverflow.com/questions/19478444/why-recursiveiteratoriterator-shows-elements-only-in-a-loop – Hanky Panky Feb 19 '14 at 09:52
  • 1
    And for attributes you'd use `foreach ($device->attributes() as $name=>$val)` – Hanky Panky Feb 19 '14 at 09:55
  • But the children of `$device->attributes` **are** unique. – Kalessin Feb 19 '14 at 09:59
  • Thanks for your help and patience with this. If I could, I'd reward you with food, beer, or multiple upvotes :) – Kalessin Feb 19 '14 at 10:15