PHP XML DOM: more than one document?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ew123
Forum Newbie
Posts: 6
Joined: Sat Jun 05, 2010 10:38 am

PHP XML DOM: more than one document?

Post by ew123 »

rssreader.php:

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>");
  }
?>
test.xml:

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>
testtwo.xml:

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>
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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP XML DOM: more than one document?

Post by Jonah Bron »

Do you get the error message when you test them individually?
ew123
Forum Newbie
Posts: 6
Joined: Sat Jun 05, 2010 10:38 am

Re: PHP XML DOM: more than one document?

Post by ew123 »

Yes the error message appears even when only one xml document is loaded.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP XML DOM: more than one document?

Post by Jonah Bron »

Replace

Code: Select all

for ($iOne=0; $iOne<=2; $iOne++)
with

Code: Select all

for ($iOne=0; $iOne<2; $iOne++)
I think that might get rid of the error message.
ew123
Forum Newbie
Posts: 6
Joined: Sat Jun 05, 2010 10:38 am

Re: PHP XML DOM: more than one document?

Post by ew123 »

Thanks, it worked a treat!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP XML DOM: more than one document?

Post by Jonah Bron »

Good. Just to make sure you understand why, <= is "less than or equal to". So, your loop was going "0, 1, 2". That's three elements. We want to stop at two. So, you can either change it to <2, or <=1
Post Reply