PHP XML DOM: more than one document?
Posted: Sat Jun 05, 2010 10:54 am
rssreader.php:
test.xml:
testtwo.xml:
Hello, this is my first post, so here goes...
I've just written this code. It is supposed to read 2 different rss feeds on one page. (The plan is later to write them into the same xml document.) However, for some reason it only reads the first feed and not the second. I have tested out both parts of the code separately and both work on their own, just not together. Is it only possible to read one xml document in one php file?
And also, for some reason this error message is displayed at the bottom of the html output:
Fatal error: Call to a member function getElementsByTagName() on a non-object in /www/sites/5a5/4ee/www.europewatch.eu/web/rssreader.php on line 9
The output can be viewed at http://www.europewatch.eu/rssreader
Thanks in advance
Code: Select all
<?php
$xmlDocOne = new DOMDocument();
$xmlDocOne->load("test.xml");
//get and output "<item>" elements
$xOne=$xmlDocOne->getElementsByTagName('item');
for ($iOne=0; $iOne<=2; $iOne++)
{
$item_titleOne=$xOne->item($iOne)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_linkOne=$xOne->item($iOne)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_descOne=$xOne->item($iOne)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href='" . $item_linkOne
. "'>" . $item_titleOne . "</a>");
echo ("<br />");
echo ($item_descOne . "</p>");
}
$xmlDocTwo = new DOMDocument();
$xmlDocTwo->load("testtwo.xml");
//get and output "<item>" elements
$xTwo=$xmlDocTwo->getElementsByTagName('item');
for ($iTwo=0; $iTwo<=2; $iTwo++)
{
$item_titleTwo=$xTwo->item($iTwo)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_linkTwo=$xTwo->item($iTwo)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_descTwo=$xTwo->item($iTwo)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href='" . $item_linkTwo
. "'>" . $item_titleTwo . "</a>");
echo ("<br />");
echo ($item_descTwo . "</p>");
}
?>Code: Select all
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>EuropeWatch</title>
<link>http://www.europewatch.eu</link>
<description>Providing in depth analysis and opinion of Eurpean current affairs</description>
<item>
<title>title 1</title>
<link>http://www.europewatch.eu</link>
<description>description 1</description>
<pubDate>04/06/2010</pubDate>
</item>
<item>
<title>title 2</title>
<link>http://www.europewatch.eu</link>
<description>description 2</description>
<pubDate>05/06/2010</pubDate>
</item>
</channel>
</rss>
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>EuropeWatch</title>
<link>http://www.europewatch.eu</link>
<description>Providing in depth analysis and opinion of Eurpean current affairs</description>
<item>
<title>First Title</title>
<link>http://www.europewatch.eu</link>
<description>First Description</description>
<pubDate>03/06/2010</pubDate>
</item>
<item>
<title>Second Title</title>
<link>http://www.europewatch.eu</link>
<description>Second Description</description>
<pubDate>06/06/2010</pubDate>
</item>
</channel>
</rss>
I've just written this code. It is supposed to read 2 different rss feeds on one page. (The plan is later to write them into the same xml document.) However, for some reason it only reads the first feed and not the second. I have tested out both parts of the code separately and both work on their own, just not together. Is it only possible to read one xml document in one php file?
And also, for some reason this error message is displayed at the bottom of the html output:
Fatal error: Call to a member function getElementsByTagName() on a non-object in /www/sites/5a5/4ee/www.europewatch.eu/web/rssreader.php on line 9
The output can be viewed at http://www.europewatch.eu/rssreader
Thanks in advance