简体   繁体   English

如何在php中获取xpath查询的结果?

[英]How to get the result of an xpath query in php?

I'm puzzled on how to extract the data from my xpath query. 我对如何从xpath查询中提取数据感到困惑。 I'm using PHP 5.5.6 and I'm getting this result: 我正在使用PHP 5.5.6,并且得到以下结果:

I'm all the countries:
DOMNodeList Object
(
    [length] => 0
)
1
I'm all the countries:
DOMNodeList Object
(
    [length] => 0
)
1
I'm all the countries:
DOMNodeList Object
(
    [length] => 0
)
1
I'm all the countries:
DOMNodeList Object
(
    [length] => 0
)
1

My XML file is (truncated to show only the first parts, I'm experimenting with large XML files): 我的XML文件(被截短以仅显示第一部分,我正在尝试使用大型XML文件):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data>
    <NewDataSet>
        <Table>
            <Country>Philippines</Country>
            <City>Subic Bay Weather Station</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Laoag</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Ninoy Aquino Inter-National Airport</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Davao Airport</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Clark Ab</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Legaspi</City>
        </Table>
        <Table>
            <Country>Philippines</Country>
            <City>Romblon</City>
        </Table>

And what I'm trying to do is to just show what's inside the Country tags via the xpath query. 我想做的就是通过xpath查询显示Country标记内的内容。 My code is: 我的代码是:

<?php

    $reader = new XMLReader();
    $reader->open("countries.xml", "UTF-8");

    while($reader->read()){
        //echo var_dump($reader->nodeType), "<br/>";
        if($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "Table"){
            $node = $reader->expand();
            $dom = new DOMDocument;
            $xp = new DomXPath($dom);
            $xp1 = $xp->query("//Country");
            echo "I'm all the countries: <pre>",print_r($xp1),"</pre>";

        }

    }

    $reader->close();


?>

I don't understand why I'm not getting a value for $xp1 that I can just use $xp1->nodeValue or $xp1->item(0)->nodeValue on. 我不明白为什么我没有得到$ xp1的值,我只能在$xp1->nodeValue$xp1->item(0)->nodeValue上使用。 I've certainly tried, even though the returned object only had "length" to give back. 我当然已经尝试过,即使返回的对象只有“长度”可以返回。 I'm looking at the example Listing 5 on this site and it looks like I should be able to do this. 我正在看此站点上的清单5示例,看来我应该能够做到这一点。 What am I missing? 我想念什么?

Your DOM is empty, you never add $node to it. 您的DOM是空的,您永远不会在其中添加$node Try: 尝试:

$reader = new XMLReader();
$reader->open("countries.xml", "UTF-8");

while($reader->read()){
    if($reader->nodeType == XMLReader::ELEMENT && $reader->localName == "Table"){
        $node = $reader->expand();
        $dom = new DOMDocument;
        $n = $dom->importNode($node, true);
        $dom->appendChild($n);
        $xp = new DomXPath($dom);
        $xp1 = $xp->query("//Country");
        echo "I'm all the countries: <pre>{$xp1->item(0)->nodeValue}</pre>";

    }

}

$reader->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM