Click to See Complete Forum and Search --> : Whitespace in XML


seohr
05-09-2006, 03:57 AM
Can someone explain something about whitespace problem in XML, i did read something about it on xml.com, but i just can't find where i got whitespace. I get this error:

XML Parsing Error: not well-formed
Location: http://www.domain.com/feed.xml
Line Number 1, Column 648:


but i can't find anything around column 648. Script for creating XML file from database was working just fine, before last couple post, after them i got problem, i can't find anything wrong with this posts or any special charachters in them.

Stephen Philbin
05-09-2006, 09:55 AM
Nobody can help without the source causing the error.

seohr
05-09-2006, 10:11 AM
This script is producing my xml file from database:

<?php
//--------------------------------------------------------------------------
// This is where you specify your Database connection stuff
//
// mysql_connect -- Open a connection to a MySQL Server / die -- Alias of exit()
//
//--------------------------------------------------------------------------
$db_name = "my_database_name"; //This is your database Name
$link = mysql_connect("localhost", "username", "pass") or die("Could not connect to server!");
//This is your table name. This is a one table config to do more table you will need to rework the code.
$table_name = 'my_table_from_database';
$broj = 20;

$select_db = mysql_select_db($db_name, $link); // mysql_select_db -- Select a MySQL database
$query = "SELECT * FROM table WHERE status = '1' ORDER BY id DESC LIMIT 20";
$result = mysql_query($query, $link) or die("Could not complete database query"); //mysql_query -- Send a MySQL query
$num = mysql_num_rows($result); //mysql_num_rows -- Get number of rows in result

if ($num != 0) {

//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
$file= fopen("feed.xml" , "w"); //fopen -- Opens file or URL

//--------------------------------------------------------------------------
// XML Header Tag Goes Here
//
//
//
//--------------------------------------------------------------------------
$_xml ='<?xml version="1.0" encoding="windows-1250"?>';
$_xml .='<rss version="2.0">';
$_xml .='<channel>';
$_xml .='<title>domain.com</title>';
$_xml .='<link>http://www.domain.com/</link>';
$_xml .='<description>Description.</description>';
$_xml .='<docs>http://www.domain.com/feed.xml</docs>';

//--------------------------------------------------------------------------
// This while loop loops throught the data found from the above query and
// generates the XML Doc.
//
//
//--------------------------------------------------------------------------
while ($row = mysql_fetch_array($result)) { //mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.
$_xml .=' <item>';
$_xml .=' <title>' . $row[title_row] . '</title>';
$_xml .=' <description>' . $row[short_description] . '</description>';
$_xml .=' <link>http://www.domain.com/url/' . $row[id] . '</link>';
$_xml .=' <guid>http://www.domain.com/url/' . $row[id] . '</guid>';
$_xml .=' <author>email@domain.com</author>';

//You can use if statements to check if the returned value is null.
//If it is just place a default value in the tags like below.
$_xml .=' </item>';
}
$_xml .='</channel>';
$_xml .='</rss>';
//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
fwrite($file, $_xml); //fwrite -- Binary-safe file write
fclose($file); //fclose -- Closes an open file pointer

//--------------------------------------------------------------------------
// This will echo the XML file out to the screen so you can view it.
//
//
//
//--------------------------------------------------------------------------
echo $_xml;
} else {
echo "No Records found";
}
?>

Can this help?

Stephen Philbin
05-09-2006, 10:32 AM
Sorry, I should have been more clear. I meant can we see the XML file please? Is it all written on one big long line? I'm assuming so, given the column number and the way the file is made by PHP. Please can we see an unaltered version of the XML file with the error. Don't edit it and put new lines in to make it more readable so that we can see exactly which character your error report is referring to.

You may also be able to solve it easily enough yourself by just asking yourself a few simple questions:

Does the output from the database contain any illegal characters or unescaped caracters?

Are there any HTML entites that are not declared by default in XML?

Can you isolate the entry causing the error and test it in a plain non-dynamic XML file?

seohr
05-09-2006, 11:24 AM
here is link to xml:

seo.hr/seohr-feed.xml

No, i can't find any illegal charachters or any html markup, just letters.

I use this script on all my domain and it works fine, i really can't say whats wrong with this xml file.

Stephen Philbin
05-09-2006, 11:45 AM
Yup. Illegal character. Literal ampersands "&" are not permitted. It must be encoded as &amp; or &#x26;.

seohr
05-09-2006, 01:15 PM
I feel little stupid now, but its working :)

Thanks for time and help.

Stephen Philbin
05-11-2006, 09:18 AM
We all learn by our mistakes and we're all allways learning. ;) Good luck with the rest of your project.

seohr
05-31-2006, 10:28 AM
Another question related to this script, this rss.php file is producing xml file, so everytime i post something i need to run manual rss.php file to see changes in xml, whats the best to do to run rss.php from my post file to refresh xml file?

quickthreads
05-31-2006, 01:01 PM
Well, if you take out the ending line:

echo $_xml;

which displays the actual xml displayed, you might be able to call this right from your post script:

include "rss.php";

Otherwise you could add a link to your "posting script" so that after you post, it shows a link to then "run" the rss.php script. That way if you are making multiple posts, you can just update the xml after you are done.

If you have access to Cron/scheduled jobs, you could also have it run automatically a few times per day.

Hope something in here helps!

-Jim