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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化