Author Topic: Getting certain data from an XML file  (Read 669 times)

adh32

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Getting certain data from an XML file
« on: August 13, 2010, 11:37:39 AM »
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:

PHP Code: [Select]
<?php
$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:

Code: [Select]
<article>
<url>http://example.com/example-page.php</url>
<title>Book review</title>
<keywords>fiction,thriller</keywords>
</article>

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.

goplano

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Getting certain data from an XML file
« Reply #1 on: August 18, 2010, 12:10:30 PM »
PHP Code: [Select]

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