为什么我的SimpleXMLElement数据被截断了?

时间:2014-02-19 09:04:56

标签: php simplexml

我检索以下XML数据:

<?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>

我将它加载到SimpleXMLElement:

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

然后我可以这样显示:

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

其中给出了以下输出:

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
                                )

                        )

                )

        )

)

到目前为止一切顺利。但我需要从DeviceInfo数组中获取数据,因此我修改了代码:

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

但是我只获得了第一个。而不是三个SimpleXMLElement对象。

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

)

我做错了什么?

更新 我首先使用print_r()的原因是因为我没有得到以下代码的输出:

<?php

$addresses = array();

foreach ($jdf_response->Response->DeviceList->DeviceInfo as $device) {
    $addresses[] = $device->{'@attributes'}'DeviceID'];
}
print_r($addresses);

1 个答案:

答案 0 :(得分:1)

  

示例#4访问SimpleXML中的非唯一元素

     

当元素的多个实例作为单个父元素的子元素存在时,   正常的迭代技术适用。

数据仍然存在,你只需要使用像:

这样的迭代器
foreach($jdf_response->Response->DeviceList->DeviceInfo as $device)
{
 print_r($device);
}

<强> Reference