2014-02-19 58 views
1

我檢索以下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); 
+0

這是一個有趣的一個 –

回答

1

實施例#4訪問在非唯一的元素SimpleXML

當元素的多個實例作爲單個父元素的子元素存在時, 正常迭代n技術適用。

數據仍然存在,你只需要使用一個迭代器,如:

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

Reference

+0

感謝。我明白爲什麼會這樣,但不是爲什麼'print_r()'不。我也不明白爲什麼'print_r($ device - > {'@ attributes'})'在foreach循環中不起作用。我會更新我的問題以反映這一點。 – Kalessin

+1

這也可以幫助你理解這種現象http://stackoverflow.com/questions/19478444/why-recursiveiteratoriterator-shows-elements-only-in-a-loop –

+1

而對於你使用'foreach($ device - > attributes()作爲$ name => $ val)' –

相關問題