0

我被这个问题困在这里。我有一个看起来像这样的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>  
<ListVehicleInventory xmlns="http://www.starstandards.org/STAR"  
xmlns:oa="http://www.openapplications.org/oagis" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.starstandards.org/STAR/STAR/Rev1.1/BODs/ListVehicleInventory.xsd" revision="1.0" release="8.1" environment="Production" lang="en-US">
<ApplicationArea>
    <Sender>
      <Component>XPO</Component>
      <Task>VehicleInventory</Task>
      <CreatorNameCode>AUTO123</CreatorNameCode>
      <SenderNameCode>XPO</SenderNameCode>
      <Language>eng</Language>
    </Sender>
    <CreationDateTime>2013-04-26T00:16:49-0400</CreationDateTime>
    <BODId>2013042601</BODId>
    <Destination>
      <DestinationNameCode>CBB</DestinationNameCode>
    </Destination>
  </ApplicationArea>
  <DataArea>
    <VehicleInventory>
      <Header>
        <TransactionType>Delete</TransactionType>
      </Header>
      <Invoice>
        <Vehicle>
          <ModelYear>2011</ModelYear>
          <Make>Dodge</Make>

...

DataArea 作为我要提取的所有车辆库存信息。我试图让 foreach 运行,但它只找到 1 个 VehicleInventory 属性,而不是通过 foreach 访问文件中的所有数据。我的 XML 中有 3 个车辆库存数据用于此测试。

这是我的实际编码:

$xml = simplexml_load_file($xmlDir . DIRECTORY_SEPARATOR . 'test.xml');  
foreach($xml as $vehicle):  
echo '<pre>';  
print_r($vehicle->VehicleInventory->Invoice->Vehicle);  
echo '</pre>';  
endforeach;

结果:这向我显示了第一个 VehicleINInventory 的所有信息,但没有循环。怎么了?

4

1 回答 1

1

循环应该是:

foreach($xml->DataArea->VehicleInventory as $vehicle)
{
    print_r($vehicle->Invoice->Vehicle);
}

您的原始代码正在循环$xml。问题在于 SimpleXML 将根节点放入其中$xml,因此在您的情况下$xml<ListVehicleInventory>

键盘演示

于 2013-06-28T14:47:53.263 回答