2014-09-29 98 views
1

所以我得到了這個XML。 我在XML中有很多類似的塊,我可以通過它循環。但是,我怎麼知道有多少塊? 或者我會如何停止後最後一個塊?到達最後一個XML元素

任何建議表示讚賞。

<StockBalanceOut xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BON_StockBalanceOut" class="entity"> 
<_DocumentHash>f5a598f180ccdecffeb7774d58ca8743</_DocumentHash> 
<AvailPhysicalAvailableQty>0</AvailPhysicalAvailableQty> 
<AvailPhysicalReservedQty>0</AvailPhysicalReservedQty> 
<AvailPhysicalReturnQty>0</AvailPhysicalReturnQty> 
<AvailPhysicalReworkQty>0</AvailPhysicalReworkQty> 
<AvailPhysicalScrapQty>0</AvailPhysicalScrapQty> 
<Date>2014-09-26</Date> 
<ItemId>15742-20907</ItemId> 
<ItemShippingClass>Empty</ItemShippingClass> 
<OnOrderQty>0</OnOrderQty> 
<PhysicalInventAvailableQty>0</PhysicalInventAvailableQty> 
<PhysicalInventReservedQty>0</PhysicalInventReservedQty> 
<PhysicalInventReturnQty>0</PhysicalInventReturnQty> 
<PhysicalInventReworkQty>0</PhysicalInventReworkQty> 
<PhysicalInventScrapQty>0</PhysicalInventScrapQty> 
<RecId>5637416600</RecId> 
<RecVersion>1</RecVersion> 
<Time>15:25:52</Time> 
</StockBalanceOut> 
<StockBalanceOut xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BON_StockBalanceOut" class="entity"> 
<_DocumentHash>6c6a3aa160f3ab9388f8e1b5b2fd7dc1</_DocumentHash> 
<AvailPhysicalAvailableQty>99</AvailPhysicalAvailableQty> 
<AvailPhysicalReservedQty>0</AvailPhysicalReservedQty> 
<AvailPhysicalReturnQty>0</AvailPhysicalReturnQty> 
<AvailPhysicalReworkQty>0</AvailPhysicalReworkQty> 
<AvailPhysicalScrapQty>0</AvailPhysicalScrapQty> 
<Date>2014-09-26</Date> 
<ItemId>21234-29752</ItemId> 
<ItemShippingClass>Empty</ItemShippingClass> 
<OnOrderQty>0</OnOrderQty> 
<PhysicalInventAvailableQty>99</PhysicalInventAvailableQty> 
<PhysicalInventReservedQty>0</PhysicalInventReservedQty> 
<PhysicalInventReturnQty>0</PhysicalInventReturnQty> 
<PhysicalInventReworkQty>0</PhysicalInventReworkQty> 
<PhysicalInventScrapQty>0</PhysicalInventScrapQty> 
<RecId>5637416601</RecId> 
<RecVersion>1</RecVersion> 
<Time>15:25:52</Time> 
</StockBalanceOut> 
+0

請顯示您的當前代碼「循環」 – Steve 2014-09-29 19:44:29

+0

當您說「blocks」時,您的意思是什麼? XML標籤? – 2014-09-29 19:44:49

+0

@Steve 我甚至無法啓動,因爲我不知道最終參數。 – user3369276 2014-09-29 19:47:15

回答

1

從您的XML中,我得到了這些信息。 有多個「塊」爲<StockBalanceOut>,您可以訪問每一個通過: -

$objectOfXMLFile->StockBalanceOut[0]; 
$objectOfXMLFile->StockBalanceOut[1]; 

爲了達到到最後,你可以運行一個while循環。如果任何索引(假設10不存在)for StockBalanceOut不存在,則它將返回null。

$counter=0; //run from 0 
    while(!is_null($xmlOBJ->StockBalanceOut[$counter])) 
    { 
//do anything here 
    $counter++; 
    } 
1

我會看看嘗試類似this。爲了節省鏈接旅程,以下是一些示例代碼,以幫助您入門。

<?php 
$xml = <<<EOF 
<people> 
<person name="Person 1"> 
    <child/> 
    <child/> 
    <child/> 
</person> 
<person name="Person 2"> 
    <child/> 
    <child/> 
    <child/> 
    <child/> 
    <child/> 
</person> 
</people> 
EOF; 

$elem = new SimpleXMLElement($xml); 

foreach ($elem as $person) { 
    printf("%s has got %d children.\n", $person['name'], $person->count()); 
} 
?> 
相關問題