dongyanfeng0546 2014-09-13 16:07
浏览 22
已采纳

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

I'm puzzled on how to extract the data from my xpath query. I'm using PHP 5.5.6 and I'm getting this result:

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 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. 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. 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. What am I missing?

  • 写回答

1条回答 默认 最新

  • doudu7626 2014-09-13 16:38
    关注

    Your DOM is empty, you never add $node to it. 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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题