doujie4344 2017-09-19 09:40
浏览 42
已采纳

循环时无法获取所有数据xml

i have problem, i want get all data from my xml file in many tags name. but when i loop, just get one in first tag. sory my english.

this is my code and sample format xml.

Code:

$xml = @simplexml_load_file('C://xampp/htdocs/logxml.xml'); 
foreach ($xml->children() as $b){ 
    echo $MakeAndModel = $b->{'xdm:ComponentGroup'}->{'xdm:Component'}->{'dd:DigitalS‌​torageTypeEnum'}; 
}

 <xdm:ComponentGroup>
 <xdm:Component id="dev1300" componentType="digitalStorage">
 <dd:DigitalStorageTypeEnum>harddisk</dd:DigitalStorageTypeEnum>
 <dd:Capacity>
 <dd:MaxCapacity>16</dd:MaxCapacity>
 <dd:Unit>bytes</dd:Unit>
 </dd:Capacity>
 </xdm:Component>
 </xdm:ComponentGroup> 

 <xdm:Component id="dev1350" componentType="digitalStorage">
 <dd:DigitalStorageTypeEnum>ram</dd:DigitalStorageTypeEnum>
 <dd:Capacity>
 <dd:MaxCapacity>1040187392</dd:MaxCapacity>
 <dd:Unit>bytes</dd:Unit>
 </dd:Capacity>
 </xdm:Component>

output = hardisk, no one else

and below of my real xml file still have many same tags name that i want to get the value. but it doesn't work. anyone help please.

  • 写回答

2条回答 默认 最新

  • dongxidui1227 2017-09-19 14:44
    关注

    There are a couple of problems, the main one is being hidden by your use of @ which is hiding any problems it may have loading the XML file. I've corrected the file so that it is now...

    <?xml version="1.0" encoding="UTF-8"?>
    <xdm:ComponentGroup xmlns:xdm="http://www.hp.com/schemas/imaging/con/xdm/1.1/"
        xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/">
        <xdm:Component id="dev1300" componentType="digitalStorage">
            <dd:DigitalStorageTypeEnum>harddisk</dd:DigitalStorageTypeEnum>
            <dd:Capacity>
                <dd:MaxCapacity>16</dd:MaxCapacity>
                <dd:Unit>bytes</dd:Unit>
            </dd:Capacity>
        </xdm:Component>
    
        <xdm:Component id="dev1350" componentType="digitalStorage">
            <dd:DigitalStorageTypeEnum>ram</dd:DigitalStorageTypeEnum>
            <dd:Capacity>
                <dd:MaxCapacity>1040187392</dd:MaxCapacity>
                <dd:Unit>bytes</dd:Unit>
            </dd:Capacity>
        </xdm:Component>
    </xdm:ComponentGroup>  
    

    I've added a few dummy url's for the namespaces, but there are probably legitimate ones you can use for your case.

    Then, using XPath to fetch the various components, you can load the file and output it using...

        <?php
        error_reporting ( E_ALL );
        ini_set ( 'display_errors', 1 );
    
        $xml = simplexml_load_file('NewFile.xml');
        $xml->registerXPathNamespace("xdm", "http://www.hp.com/schemas/imaging/con/xdm/1.1/");
        $xml->registerXPathNamespace("dd", "http://www.hp.com/schemas/imaging/con/dictionaries/1.0/");
        $components = $xml->xpath("//xdm:Component");
        foreach ($components as $component){
            $digitalStorageTypeEnum = $component->xpath("descendant::dd:DigitalStorageTypeEnum/text()")[0];
            echo $component['id']."-". $digitalStorageTypeEnum.PHP_EOL;
        }
    

    This registers the namespaces as it references them in the XPath expressions. So it first looks for all <xdm:Component> elements and then within that it fetches the dd:DigitalStorageTypeEnum elements. Although the text() is only one piece of text, xpath always returns an array, so just take the first result as the content.

    This outputs:

    dev1300-harddisk
    dev1350-ram
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写