0

下面的代码应该打印:PHP XPath Example,但它没有。我不确定为什么。

文章.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>

测试.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
}
4

1 回答 1

1

它应该工作:

$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";
}
于 2012-10-28T02:10:26.580 回答