Forum Moderators: coopster
What I am trying to achieve is to create a RSS newsfeed, basically I get the news items from a DB, and then write to an XML file.
When I do run this script below, I get the is error:
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
XML document must have a top level element. Error processing resource
Now is it because there is use of invalid XML tags? I just don't know the answer.
Any Ideas
W :-)
/****************************
Description Variables
****************************/
$_ttl = 'Company name';
$_des = 'Latest News';
$_lnk = 'http://www.mydomain.com';
$_cop = '© Copyright 2004 All rights reserved';
// Need to get current date
$_bld = '17 March 2004';
$_wbm = 'webmaster@mydomain.com';
/****************************
Initialize data variable
****************************/
$_xml = ( bool ) false;
/****************************
Create Data
****************************/
$_xml .= '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n\n";
$_xml .= '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">' . "\n";
$_xml .= '<rss version="0.91">' . "\n";
$_xml .= '<channel>' . "\n";
$_xml .= '<language>en-us</language>' . "\n";
$_xml .= '<title>' . $_ttl . '</title>' . "\n";
$_xml .= '<description>' . $_des . '</description>' . "\n";
$_xml .= '<link>' . $_lnk . '</link>' . "\n";
$_xml .= '<copyright>' . $_cop . '</copyright>' . "\n";
$_xml .= '<lastBuildDate>' . $_bld . '</lastBuildDate>' . "\n";
$_xml .= '<webMaster>' . $_wbm . '</webMaster>' . "\n";
/********************************
Number of records to return
********************************/
$_count = 15;
/********************************
DB Connection
********************************/
// open database
include ("connectt.php");
/********************************
DB Query
********************************/
$result = mysql_query( "select itemid,heading,prop_date,content
from newsfeed
LIMIT $_count" );
/********************************
Create the syndication
********************************/
while (list($DBitemid,$DBheading,$DBpropdate,$DBcontent)=mysql_fetch_row($result))
{
$_xml .= '<item>' . "\n";
$_xml .= '<itemid>' . "\n";
$_xml .= $DBitemid . "\n";
$_xml .= '</itemid>' . "\n";
$_xml .= '<title>' . "\n";
$_xml .= $DBheading . "\n";
$_xml .= '</title> ' . "\n";
$_xml .= '<itemdate>' . "\n";
$_xml .= $DBpropdate . "\n";
$_xml .= '</itemdate> ' . "\n";
$_xml .= '<description>' . "\n";
$_xml .= $DBcontent . "\n";
$_xml .= '</description>' . "\n";
$_xml .= '</item>' . "\n";
}
$_xml .= '</channel>' . "\n";
$_xml .= '</rss>' . "\n";
/***********************************
Write XML to file
***********************************/
$_file = 'site_feed.xml';
$_fp = @fopen( $_file, 'w' );
$_fout = @fwrite( $_fp, $_xml );
@fclose( $_fp );
/***********************************
Return File to user
***********************************/
header( 'location: site_feed.xml' );
exit;
Okay, I've been playing around with this and it seems to be having a problem writing to the file. I have placed '@' in front of the file commands.
I've also changed the filename to test.txt, and this has proved to be the case.
This is why I get this XML error because there is no XML commands in there.
Any further ideas?
Thanks