XML simplexml_load_file foreach不循环 [英] XML simplexml_load_file foreach not looping

查看:147
本文介绍了XML simplexml_load_file foreach不循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了这个问题。
我有一个XML文件,看起来像这样:

 <?xml version =1.0encoding = 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.0release =8.1environment =Productionlang =en-US>
< ApplicationArea>
<发件人>
< Component> XPO< / Component>
<任务> VehicleInventory< /任务>
< CreatorNameCode> AUTO123< / CreatorNameCode>
< SenderNameCode> XPO< / SenderNameCode>
< Language> eng< / Language>
< /发件人>
< CreationDateTime> 2013-04-26T00:16:49-0400< / CreationDateTime>
< BODId> 2013042601< / BODId>
< Destination>
< DestinationNameCode> CBB< / DestinationNameCode>
< / Destination>
< / ApplicationArea>
< DataArea>
< VehicleInventory>
<标题>
< TransactionType>删除< / TransactionType>
< / Header>
<发票>
<车辆>
< ModelYear> 2011< / ModelYear>
<制作>道奇< / Make>

...



DataArea as all我想要退出的车辆库存信息。我想要得到一个foreach去,但它只能找到1 VehicleInventory属性,而不是通过文件中的所有数据的foreach。我有3个vehicleInventory数据在我的XML中用于这个测试。

这是我的实际编码:

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

结果:这显示了第一个VehicleInventory的所有信息,但doens't循环。什么是错误的?

解决方案

循环应该是:

<$ p $ ($ xml-> DataArea-> VehicleInventory as $ vehicle)
{
print_r($ vehicle-> Invoice-> Vehicle);



$ b

您的原始代码正在循环 $ xml code>。问题是SimpleXML将根节点放在 $ xml 中,所以在你的情况下 $ xml < ListVehicleInventory>



键盘演示

I am stuck here with this problem. I have an XML file that LOOKS LIKE THIS:

<?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 as all the vehicle Inventory information that I want to pull out. I am trying to get a foreach going but it only find 1 VehicleInventory attributes and not going thru the foreach to all the data in the file. I have 3 vehicleInventory data in my XML for this testing.

Here is my actual coding:

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

Result: THis shows me all the info for the 1st VehicleINventory but doens't loop. What is wrong?

解决方案

The loop should be:

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

Your original code is looping over $xml. The problem with that is SimpleXML puts the root node in $xml, so in your case $xml is <ListVehicleInventory>.

Codepad Demo

这篇关于XML simplexml_load_file foreach不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆