到达最后一个XML元素


Reaching the last XML element

所以我得到了这个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>

从您的XML中,我得到了这些信息。<StockBalanceOut>有多个"块",您可以通过以下方式访问每个块:-

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

要到达终点,你可以运行一个while循环。如果StockBalanceOut的任何索引(假设10不存在)都不存在,那么它将返回null。

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

我会考虑尝试这样的东西。为了节省您的链接之旅,这里有一些示例代码可以帮助您开始。

<?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());
}
?>