Page 1 of 1

PHP creating XML output problem

Posted: Wed Dec 21, 2011 9:07 am
by JoeSyn
Hi everyone,

would it be possible please to have a look at the below PHP - it is receiving 8 variables but I receive an error saying that the file must be "well formed" and "only one top level element is allowed in an XML document". Any ideas as to what the problem could be?

Code: Select all

<?php

header("Content-type: text/xml");

$artist =$_POST['artist'];
$medium =$_POST['medium'];
$size =$_POST['size'];
$format =$_POST['format'];
$subject =$_POST['subject'];
$colour =$_POST['colour'];
$price =$POST['price'];
$available =$POST['available'];

$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';

$xmlBody .= "<XML>";

$host = "127.0.0.1";
$user = "root";
$pass = "";
$database = "syntegallery";

$con = mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($database, $con) or die ("Could not find database");

$query = "SELECT Filename, Thumbnail, Empirical";
$query .= "FROM gallery";
$query .= "WHERE Artist='$artist'";
$query .= "AND Medium='$medium'";
$query .= "AND Category='$size'";
$query .= "AND Format='$format'";
$query .= "AND Subject='$subject'";
$query .= "AND Colour='$colour'";
$query .= "AND Range='$price'";
$query .= "AND Available='$available'";
$query .= "ORDER BY Empirical";

$result = mysql_query($query, $con) or die ("Data not found");

$index = 0;

while($row = mysql_fetch_array($result))
{
	$Filename = $row["Filename"];
	$Thumbnail = $row["Thumbnail"];
	$Empirical = $row["Empirical"];
	$index++;
	$xmlBody .= '
<gallery>
	<filename>'.$Filename.'</filename>
	<thumbnail>'.$Thumbnail.'</thumbnail>
	<empirical>'.$Empirical.'</empirical>
</gallery>';
}

$xmlBody .= "</XML>";

print $xmlBody;
mysql_close($con);
exit();
?>
Thanks so much!

Re: PHP creating XML output problem

Posted: Wed Dec 21, 2011 1:49 pm
by tr0gd0rr
Can you post the xml output? Is it possible that filename, thumbnail, or empirical contain characters that need to be escaped such as quote, apostrophe, ampersand, less than, or greater than?

Re: PHP creating XML output problem

Posted: Wed Dec 21, 2011 3:44 pm
by JoeSyn
Hey thanks for the reply. The error reads as follows:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

Only one top level element is allowed in an XML document. Error processing resource 'http://127.0.0.1/getImage.php'. Line ...

<b>Notice</b>: Undefined index: artist in <b>C:\Program Files (x86)\EasyPHP-5.3.8.1\www\getImage.php</...

Unfortunately the variables being sent to PHP (for eg size: 1m² < and price: R20,000 <) do have greater and less than signs and the returned empirical data would read as follows: Artist, "Title". Medium, Size(eg. 100 x 100cm), Price. The Filename and Thumbnail results are image URLs.

Wow that would not have been something I would have considered in a hurry. Do these need to be changed to say for instance '1m² or greater' and take out the quotations?

Re: PHP creating XML output problem

Posted: Thu Dec 22, 2011 2:05 am
by JoeSyn
I have replaced all values in the database and search fields to exclude any quotes and less/greater than values ... but same error.

Dreamweaver output the following:

This page contains the following errors:
error on line 2 at column 1: Extra content at the end of the document

Flash output the following:

Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

I'm not sure if this could give a better indication of where the problem could be but I'm truly stuck

Re: PHP creating XML output problem

Posted: Thu Dec 22, 2011 1:48 pm
by tr0gd0rr
If you look at the source of the page it should show you the xml. It would be helpful to see the xml.

You want to encode special characters to character references such as < to <. Here is a untested and perhaps oversimplified function to escape entities for xml:

Code: Select all

function xmlentities($s) {
    return str_replace(''', '&apos;', htmlentities($s));
}
Another option is to use CDATA sections:

Code: Select all

$xmlBody .= '
<gallery>
        <filename><![CDATA['.$Filename.']]></filename>
        <thumbnail><![CDATA['.$Thumbnail.']]></thumbnail>
        <empirical><![CDATA['.$Empirical.']]></empirical>
</gallery>';

Re: PHP creating XML output problem

Posted: Sat Dec 24, 2011 2:49 am
by JoeSyn
Thanks tr0gd0rr,

It turns out that the word range was being picked up MySQL as a keyword and needed to be placed between back ticks. I also removed the $query variable to $result (mysql_query) because I think it was conflicting and returning boolean results. Thanks so much for your time though!

Here is the working code:

Code: Select all

<?php
//set the content type to XML
header("Content-type: text/xml");
//initialise the xmlBody variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
//start XMLBody output
$xmlBody .= "<XML>";

//define the variables to be received from user input
$artist =$_POST['artist'];
$medium =$_POST['medium'];
$size =$_POST['size'];
$format =$_POST['format'];
$subject =$_POST['subject'];
$colour =$_POST['colour'];
$price =$_POST['price'];
$available =$_POST['available'];

//define database connection variables
$host = "127.0.0.1";
$user = "root";
$pass = "";
$database = "syntegallery";

//connect to the database and define its name
$con = mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($database, $con) or die ("Could not find database");

//define the mysql query
$result = mysql_query("SELECT Filename, Thumbnail, Empirical FROM gallery WHERE Artist='$artist' AND Medium='$medium' AND Category='$size' AND Format='$format' AND Subject='$subject' AND Colour='$colour' AND `Range`='$price' AND Available='$available' ", $con) or die ("Data not found");

//create incremental counter
$i = 0;

//create while loop to search until all rows have been checked against query and define XML structure
while($row = mysql_fetch_array($result))
{
	$Filename = $row["Filename"];
	$Thumbnail = $row["Thumbnail"];
	$Empirical = $row["Empirical"];
	$i++;
	$xmlBody .= '
<gallery>
	<filename>'.$Filename.'</filename>
	<thumbnail>'.$Thumbnail.'</thumbnail>
	<empirical>'.$Empirical.'</empirical>
</gallery>';
}//end the while loop

//end XmlBody output
$xmlBody .= "</XML>";
//print xmlBody to be parsed and displayed to user
print $xmlBody;
//close mysql connection
mysql_close($con);
exit();
?>