XML PHP获取子项值

时间:2016-05-23 20:37:50

标签: php xml simplexml

XML:

    <Result xmlns="" xmlns:xsi="" totalResultsAvailable="0" totalResultsReturned="0" schk="true" totalLooseOffers="0" xsi:schemaLocation="">
        <details>
            <ID></ID>
            <applicationVersion>1.0</applicationVersion>
            <applicationPath/>
            <date>2016-05-23T12:17:16.369-03:00</date>
            <elapsedTime>17</elapsedTime>
            <status>success</status>
            <message>success</message>
        </details>
        <category id="1">
        <thumbnail url="http://image.google.com/test.jpg"/>
        <links>
            <link url="www.google.com" type="category"/>
            <link url="www.google2.com" type="xml"/>
        </links>
        <name>Category</name>
        <filters>
            <filter id="1" name="Filter1">
               <value id="1" value="Test1"/>
               <value id="2" value="Test2"/>
               <value id="3" value="Test3"/>
            </filter>
           <filter id="2" name="Filter2">
               <value id="1" value="Test4"/>
               <value id="2" value="Test5"/>
               <value id="3" value="Test6"/>
            </filter>
        </filters>
        </category>
</Result>

PHP:

  $xml = simplexml_load_file("http://xml.com");
    foreach($xml->category->filters as $filters){
           foreach($filters->children() as $child){
                   echo $child['value'];
           }
    }

我正在尝试获取过滤器值,但我的代码没有显示任何内容。我看到了一些关于xpath的内容,但不知道它是否适用于这种情况。你有什么线索吗?

-

当XML看起来像这样:

<Result xmlns="" xmlns:xsi="" totalResultsAvailable="0" totalResultsReturned="0" schk="true" totalLooseOffers="0" xsi:schemaLocation="">
        <details>
            <ID></ID>
            <applicationVersion>1.0</applicationVersion>
            <applicationPath/>
            <date>2016-05-23T12:17:16.369-03:00</date>
            <elapsedTime>17</elapsedTime>
            <status>success</status>
            <message>success</message>
        </details>
        <subCategory id="1">
           <thumbnail url="http://image.google.com/test.jpg"/>
           <name>Subcategory</name>
        </subCategory>
        <subCategory id="2">
           <thumbnail url="http://image.google.com/test2.jpg"/>
           <name>Subcategory2</name>
        </subCategory>
</Result>

然后我可以这样做:

foreach($xml->subCategory as $subCategory){
        $categoryId   = $subCategory['id'];
        $categoryName = $subCategory->name;
} 

1 个答案:

答案 0 :(得分:2)

您在内部循环中引用为$child的元素实际上指向<filter>节点,而不是您尝试为其定义属性的子节点<value>。所以这只是扩展外部foreach循环以迭代$xml->category->filters->filter而不是其父$xml->category->filters的问题。

// Iterate the correct <filter> node, not its parent <filters>
foreach ($xml->category->filters->filter as $filter) {
    foreach($filter->children() as $child){
        echo $child['value'] . "\n";
    }
}

这是示范:https://3v4l.org/Rqc4Y

使用xpath,您可以直接定位内部节点。

$values = $xml->xpath('//category/filters/filter/value');
foreach ($values as $value) {
    echo $value['value'];
}

https://3v4l.org/vPhKE

这两个例子都输出

Test1
Test2
Test3
Test4
Test5
Test6