我檢索以下XML數據:爲什麼我的SimpleXMLElement數據被截斷?
<?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);
這是一個有趣的一個 –