I am trying to parse data from an xml or rss file. It is a bit of a mess. People tell me that its meant to be easy but I am struggling alot.
First I want to get the radio shows news to display on site. I got the rss feeds and api both from an external source and they update the xml content daily and I just need to get the file and save it on my server so I can read off it.
Here is a sample of the rss.
<?xml version="1.0"?>
<channel>
<title>external source</title>
<link>http://www.externalsource.com/</link>
<description>external source</description>
<language>en-us</language>
<pubDate>Thu, Jun 3 2011 10:23:56 PDT</pubDate>
<ttl>60</ttl>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<item>
<title>08:00 pm</title>
<link>http://www.externalsource.com</link>
</item>
<item>
<title>- Stephen Ace show</title>
<link>http://www.externalsource.com/stephen_ace</link>
<description>Stephens show</description>
</item>
<item>
<title>- Sarah Hardy show</title>
<link>http://www.externalsource.com/sarah_hardy</link>
<description> Sarah's first show in her new slot.</description>
</item>
<item>
<title>- Radio 4 evening show</title>
<link>http://www.externalsource.com/shows/id-453</link>
<description>Bill Grady is supported by co-host Lenny Hillroy</description>
</item>
<item>
<title>- Kiss music evening show will Sady</title>
<link>http://www.externalsource.com/shows/id-112</link>
<description>Sady presents the evening show here at Kiss.</description>
</item>
</channel>
I save this file as tonight.xml and it updates evening 24 hours from a PHP script which uses fread().
I want to show just the titles of the shows that are playing that evening, nothing else.
I am ok with MySQL but am really not getting this. I am very stuck.
Here is my php
<?php
$thexml = simplexml_load_file("tonight.xml");
echo $thexml->getName() . "<br />";
foreach($thexml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
When I test it it does not print the correct values.
A quick solution using the
SimpleXMLElement::xpath()function:You find more Xpath resources here:
Next to SimpleXML there is also DOMDocument, the big sister of it:
That should work
You will get it, just focus on filling in all the gaps you find in your knowledge and you will get there.
Read up on DomDocument and DomXPath in them PHP manual here: