www.webdeveloper.com
Results 1 to 7 of 7

Thread: xml parsing error when generating an rss file with php

  1. #1
    Join Date
    May 2005
    Posts
    76

    xml parsing error when generating an rss file with php

    Hi,
    when i try to generate an rss file with php i get the error:

    XML Parsing Error: XML or text declaration not at start of entity
    Location: http://hermes.hud.ac.uk/u0759062/index.php
    Line Number 25, Column 1:

    Line 25 is the <?php tag.

    After already researching this problem I found that it came about from lines of space between code outside of the php tags. However, I have removed all the lines of space i could possibly find, and i am still recieving the error.

    Can somebody see what I'm doing wrong here? sorry if its an easy, fix; I'm a bit of a php newbie. Here's the code of the entire page (note there are no spaces within the included css file either):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>nutman's home page</title>
    <link href="style.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
    <div id="wholepage">
    <div id="header">
    <img src="banner.jpg" align="top" alt="nutman"/>
    </div>
    <div id="navigation">
    <ul>
    <li><a id="home" href="index.php">home</a></li>
    <li><a id="music" href="music.html">music</a></li>
    <li><a id="bio" href="bio.html">bio</a></li>
    <li><a id="gallery" href="gallery.html">gallery</a></li>
    <li><a id="login" href="admin.php">links</a></li>
    <li><img src="footer.jpg" alt="footer"/></li>
    </ul>
    <div id="content">
    <h2>News</h2>
    <?php
    $host='localhost';
    $user='u0759062';
    $pass='password';
    mysql_connect($host,$user,$pass);
    mysql_select_db($user);
    header('Content-type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    echo '
    <rss version=\'2.0\'> ';
    echo '
    <channel>
    <title>Nutman</title>
    <description>Electronic Music Producer</description>
    <link>http://hermes.hud.ac.uk/u0759062/</link>';
    $query="select date, title, text, author, music from newstable order by date";
    $result=mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
    echo '
    <item>
    <link>http://hermes.hud.ac.uk/u0759062/#'.$row['title'].'</link>
    <title>'.$row['title'].'</title>
    <description>'.$row['text'].'</description>
    <enclosure url="http://hermes.hud.ac.uk/u0759062/#'.$row['music'].'"/>
    </item>';
    }
    echo '
    </channel>
    </rss>';
    while ($row=mysql_fetch_array($result)){
    echo $row['date'];
    echo ' - ';
    echo '<a href="showNews.php?title='.$row['title'].'">'.$row['title'].'</a>';
    echo '<br><br>';
    }
    mysql_close();
    ?> 
    </div>
    </div>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Looks like you are trying to generate a XML document (the RSS) within the middle of a XHTML document. That's not going to work. The RSS document will need to be served up as a separate XML file.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2006
    Posts
    1,906
    This error appears to come from your <?XML.... statement at the start of the rss file, not your <?PHP statement. Did you intend on echoing the RSS to the browser, or are you trying to make a file from it?

    Dave

  4. #4
    Join Date
    May 2005
    Posts
    76
    bascially what im trying to do is create an rss feed that is automatically updated when I add something new to the database. I was under the impression that what I have written would do that!

    yeh tracknut, your right, but when it states what line its referring to in the error (line 25).. it points the php tag for some reason, as that is all that is on line 25.

  5. #5
    Join Date
    Aug 2006
    Posts
    1,906
    Quote Originally Posted by danz321 View Post
    bascially what im trying to do is create an rss feed that is automatically updated when I add something new to the database. I was under the impression that what I have written would do that!

    yeh tracknut, your right, but when it states what line its referring to in the error (line 25).. it points the php tag for some reason, as that is all that is on line 25.
    What you're doing generally looks fine, just replace your "echo" statements with the relevant "fopen" and "fwrite" or any other number of ways to write to a file instead.

    Rule #17 of programming: take any line numbers with a grain of salt

    Dave

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    The error you are seeing is not a PHP error. It is a client-side error from your browser trying to interpret the output you are sending to it. To reiterate, you cannot have an XML document embedded in the middle of a XHTML document. All the RSS generation stuff needs to be in a separate PHP file that only outputs the XML.

    If you want that data displayed within your main XHTML document, then you'll need to load the XML by accessing it via a URL (or by including it with output buffering and then dumping the buffer to a variable) and parse/process its output into the desired XHTML output.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2005
    Posts
    76
    ok, i have now changed the code, and im getting the error "can't open file", this is becuase the xml file im opening to write wont open, and im not sure why. here's the code, any ideas why it wont work?

    Code:
    <?php
    $host='localhost';
    $user='u0759062';
    $pass='password';
    mysql_connect($host,$user,$pass);
    mysql_select_db($user);
    $query="select date, title, text, author, music from newstable order by date";
    $result=mysql_query($query);
    
    $myFile = "rss.xml";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $stringData .= "<rss version=\'2.0\'>\n";
    $stringData .= "<channel>\n";
    $stringData .= "<title>Nutman</title>\n";
    $stringData .= "<description>Electronic Music Producer</description>\n";
    $stringData .= "<link>http://hermes.hud.ac.uk/u0759062/</link>\n";
    while($row = mysql_fetch_array($result)) {
    $stringData .= "<item>\n";
    $stringData .= "<link>http://hermes.hud.ac.uk/u0759062/#".$row['title']."</link>\n";
    $stringData .= "<title>".$row['title']."</title>\n";
    $stringData .= "<description>".$row['text']."</description>\n";
    $stringData .= '<enclosure url="http://hermes.hud.ac.uk/u0759062/#'.$row['music'].'"/>\n';
    $stringData .= "</item>';\n";
    $stringData .= "</channel>\n";
    $stringData .= "</rss>\n";
    fwrite($fh, $stringData);
    fclose($fh);
    }
    
    while ($row=mysql_fetch_array($result)){
    echo $row['date'];
    echo ' - ';
    echo '<a href="showNews.php?title='.$row['title'].'">'.$row['title'].'</a>';
    echo '<br><br>';
    }
    mysql_close();
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles