到达最后一个XML元素

时间:2014-09-29 19:41:40

标签: php 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>

2 个答案:

答案 0 :(得分: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());
}
?>

答案 1 :(得分:1)

从您的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++;
    }