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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler