Exclude children based on content in XML with PHP (simplexml)

Posted by Hakan on Stack Overflow See other posts from Stack Overflow or by Hakan
Published on 2011-01-09T04:47:08Z Indexed on 2011/01/09 4:54 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

Another question about PHP and XML...

Is it posible to exclude children based on there childrens content.

See the example below: If "title" contains the word "XTRA:" I don't want this "movie" to be listed.

This is my PHP code:

<? $xml = simplexml_load_file("movies.xml");
foreach ($xml->movie as $movie){ ?>

<h2><? echo $movie->title ?></h2>
<p>Year: <? echo $movie->year ?></p>

<? } ?>

This is mys XML file:

<?xml version="1.0" encoding="UTF-8"?>
<movies>
    <movie>
        <title>Little Fockers</title>
    <year>2010</year>
</movie>
<movie>
    <title>Little Fockers XTRA: teaser 3</title>
    <year>2010</year>
</movie>
</movies>

The outcome of the code above is:

<h2>Little Fockers</h2>
<p>Year: 2010</p>

<h2>Little Fockers XTRA: teaser 3</h2>
<p>Year: 2010</p>

I want it to be only:

<h2>Little Fockers</h2>
<p>Year: 2010</p>

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml