Getting certain data from an XML file

I have been struggling with this for quite a long time. I want to extract some data from an XML file (which I’ve written) and display it as part of a web page.

I know how to fetch all of the data and put it into HTML elements, but I need to display only some of it based on certain matches. For example; if I write a book review, I would want to display links to other related book reviews. I can’t really explain it any better than that. Here is what I have so far:

$category = ‘thriller’;
$xml = simplexml_load_file(‘articlesxml.xml’);
// …and I’m stuck!

Each section of the XML file is formatted as follows, but I could easily change it if doing so would make it easier to fetch the data:


Book review fiction,thriller [/code]

I have read a few SimpleXML tutorials on the internet, but I haven’t found the answer to this. They all seem to focus on fetching all occurrences of one XML element.


<?php $category = 'thriller'; $xml = simplexml_load_file('articlesxml.xml'); $results = $xml->xpath("//article/keywords[contains(.,'".$category."')]"); print_r($results); ?>


Sponsor our Newsletter | Privacy Policy | Terms of Service