views:

182

answers:

3

Hi,

I am pulling content from a xml file with simpleXML

I was wondering if it is possible to display a certain node depending on the contents of the node.

eg.

<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 = PHP

I really hope this is possible.

Thanks to anyone who replies

+3  A: 
$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.

zacharydanger
+5  A: 

You could use an XPath expression like //article[title='PHP']/content

Richard M
+3  A: 

As follow up to @Triffid - see in PHP DevCenter, here is a sample:

$article_list = new SimpleXMLElement($article_xml);  
foreach ($article_list->xsearch('//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.

Dror