Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中获得xpath查询的结果?_Php_Xml_Domxpath - Fatal编程技术网

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

如何在php中获得xpath查询的结果?,php,xml,domxpath,Php,Xml,Domxpath,我对如何从xpath查询中提取数据感到困惑。我使用的是PHP5.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

我对如何从xpath查询中提取数据感到困惑。我使用的是PHP5.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
我的XML文件被截断(仅显示第一部分,我正在试验大型XML文件):


菲律宾
苏比克湾气象站
菲律宾
拉奥格
菲律宾
尼诺阿基诺国际机场
菲律宾
达沃机场
菲律宾
克拉克Ab
菲律宾
莱加斯皮
菲律宾
龙勃龙
我试图做的是通过xpath查询显示Country标记中的内容。我的代码是:

<?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();


?>

您的DOM是空的,您永远不会向其添加
$node
。尝试:

$reader=newXMLReader();
$reader->open(“countries.xml”、“UTF-8”);
而($reader->read()){
如果($reader->nodeType==XMLReader::ELEMENT&&$reader->localName==“表”){
$node=$reader->expand();
$dom=新的DOMDocument;
$n=$dom->importNode($node,true);
$dom->appendChild($n);
$xp=新的DomXPath($dom);
$xp1=$xp->query(“//国家”);
echo“我是所有国家:{$xp1->item(0)->nodeValue}”;
$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();
} } $reader->close();
它可以工作!这是有道理的。我对必须使用appendChild()感到困惑,因为我认为可能要修改原始XML文件,但现在我明白了,我这样做只是为了填充DOM,以便使用xpath。谢谢
$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();