簡體   English   中英

如何在這里使用xpath和php獲取節點值?

[英]How do I get the node value using xpath and php here?

應該打印以下代碼:PHP XPath示例,但不是。 我不知道為什么。

article.xml:

<?xml version="1.0" encoding="UTF-8"?>
<articles>
    <article id="1">
        <tags>
            <tag>php</tag>
            <tag>xpath</tag>
        </tags>
        <title>PHP XPath Example</title>
    </article>
    <article id="2">
        <tags>
            <tag>dom</tag>
            <tag>dodocument</tag>
        </tags>
        <title>DomDocument Tutorial</title>
    </article>
</articles>

test.php:

<?php
require 'krumo/class.krumo.php';
//phpinfo();
$file = "/home/veronzhg/public_html/venturelateral.com/pi_candidate_project/articles.xml";

$xml = simplexml_load_file($file);
krumo($xml);
$arts = $xml->xpath("/articles/article/title");

krumo($arts);
foreach ($arts as $art)
{
    echo $art->textContent ." printed <br />";
    //nodeValue
}

它應該工作:

$stringxml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<articles>
    <article id="1">
        <tags>
            <tag>php</tag>
            <tag>xpath</tag>
        </tags>
        <title>PHP XPath Example</title>
    </article>
    <article id="2">
        <tags>
            <tag>dom</tag>
            <tag>dodocument</tag>
        </tags>
        <title>DomDocument Tutorial</title>
    </article>
</articles>

EOD;

$xml = new SimpleXMLElement($stringxml);

foreach ($xml->xpath('/articles/article/title') as $title)
{
    echo "$title\n";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM