Reputation: 2951
I am pulling content from an XML file with SimpleXML.
I was wondering if it is possible to display a certain node depending on the contents of the node, e.g.
<article>
<title>PHP</title>
<content>yada yada yada</content>
</article>
<article>
<title>JAVASCRIPT</title>
<content>yodo yodo yodo</content>
</article>
Can SimpleXML find a specific title and then display the article for that title?
Display article whose title is 'PHP'.
I really hope this is possible.
Thanks to anyone who replies.
Upvotes: 0
Views: 549
Reputation: 7305
See PHP DevCenter, here is a sample:
$article_list = new SimpleXMLElement($article_xml);
foreach ($article_list->xpath("//article[title='PHP']/content") as $content) {
print "$content\n";
}
Also if you know the exact location of the article nodes it is better to avoid the //
notation which will search in all levels of the XML.
Upvotes: 3
Reputation: 14535
You could use an XPath expression like //article[title='PHP']/content
Upvotes: 6
Reputation: 514
$article_list = new SimpleXMLElement($article_xml);
foreach($article_list->article as $i => $article) {
if('PHP' == $article->title) {
//code to display article.
}
}
This is assuming the article tags are in a parent element.
Upvotes: 3