-2

以下 xml 是从我无法控制的 Web 服务返回的缩短的示例 xml。有人能告诉我如何使用 simplexml 先读取 stats 中的记录数,然后正确迭代设备项目,以便我可以显示项目价格和数量吗?谢谢。

    <?xml version="1.0"?>
    <response>
     <inventory>
      <warehouse>
       <stats> 
        <location>Main</location>
        <reportTime>3/9/2013 16:50 PM</reportTime>
        <records>2</records>
       </stats>
       <equipment>
        <item>
         <model>B14</model>
         <status>Active</status>
         <price>410.33</price>
         <color>blue</color>
         <qty>100</qty>
        </item>
       </equipment>
       <equipment>
        <item>
         <model>C15</model>
         <status>Active</status>
         <price>199.10</price>
         <color>red</color>
         <qty>200</qty>
        </item>
       </equipment>
      </warehouse>
     </inventory>
    </response>
4

1 回答 1

1
<?php
$xml = ...; // Your XML

$sx = new SimpleXMLElement($xml);
$numRecords = $sx->inventory->warehouse->stats->records;

foreach($sx->inventory->warehouse->equipment as $item) {
    echo $item->item->price,', ',$item->item->qty,'<br />';
}
?>

输出:

410.33, 100

199.10, 200

于 2013-04-10T17:16:57.007 回答