PHP XML DOM more problems

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 problems

Post by ew123 »

rssreader.php

Code: Select all

<?php

//connect to database
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("europewatch_eu_db", $con);

//wipes the rss table to prevent duplicates
mysql_query("DELETE FROM rss");

//loads first rss feed
$xmlDocOne = new DOMDocument();
$xmlDocOne->load("test.xml");

//gets "<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;
  $item_dateOne=$xOne->item($iOne)->getElementsByTagName('pubDate')
  ->item(0)->childNodes->item(0)->nodeValue;

//inserts "<item>" elements into table
mysql_query("INSERT INTO rss (title, link, description, pubDate)
VALUES ('$item_titleOne', '$item_linkOne', '$item_descOne', '$item_dateOne')");

  }

//loads second rss feed
$xmlDocTwo = new DOMDocument();
$xmlDocTwo->load("testtwo.xml");

//get and insert "<item>" elements into table
$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;
  $item_dateTwo=$xTwo->item($iTwo)->getElementsByTagName('pubDate')
  ->item(0)->childNodes->item(0)->nodeValue;

mysql_query("INSERT INTO rss (title, link, description, pubDate)
VALUES ('$item_titleTwo', '$item_linkTwo', '$item_descTwo', '$item_dateTwo')");

  }

mysql_select_db("europewatch_eu_db", $con);

//reads from rss table
$result = mysql_query("SELECT * FROM rss ORDER BY PubDate");

//creates new xml document
$doc = new DomDocument('1.0', 'UTF-8');

//inserts <rss>
$rss = $doc->createElement('rss');
$rss = $doc->appendChild($rss);

//inserts <channel>
$chn = $doc->createElement('channel');
$chn = $doc->appendChild($chn);

// process one row at a time
while($row = mysql_fetch_assoc($result)) {

  // add node for each row
  $itm = $doc->createElement('item');
  $itm = $root->appendChild($itm);

  // add a child node for each field
  foreach ($row as $fieldname => $fieldvalue) {

    $child = $doc->createElement($fieldname);
    $child = $itm->appendChild($child);

    $value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);

  } // foreach
} // while

// get completed xml document
$xml_string = $doc->saveXML();

echo $xml_string;

?>
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>
The 'rss' table in the mysql database contains the field ID, title, link, description and pubDate.

Hello,

I am attempting to merge two rss feeds into one single rss feed by reading the two xml files (with XML DOM), storing their values in a mysql database and then outputting (with XML DOM) the stored values as one xml document. It works fine up to the bit where the database table is output as an xml document. I get the following error message:

Fatal error: Call to a member function appendChild() on a non-object in /www/sites/5a5/4ee/www.europewatch.eu/web/rssreader.php on line 81

(this can be viewed at http://www.europewatch.eu/rssreader)

Does anybody know exactly what has gone wrong?

Thanks in advance.
Post Reply