Reputation: 2464
The below code is supposed to print: PHP XPath Example, but it doesn't. I'm not sure why.
articles.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
}
Upvotes: 0
Views: 168
Reputation: 13559
It should work:
$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";
}
Upvotes: 1