tags:

views:

157

answers:

5

Hello,

I have a php script that writes xml data to a file and another one that sends the contents of this file to the client as the response. But on the client side,im getting the following error: XML Parsing Error: not well-formed When i view source of the page, the XML i see is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books><date>December 24th, 2009</date><total>2</total><book><name>Book 1</name><url>http://www.mydomain.com/posters/68370/img.jpg&lt;/url&gt;&lt;/book&gt;&lt;book&gt;&lt;name&gt;Book 2</name><url>http://www.anotherdomain.com/posters/76198/img1.jpg&lt;/url&gt;&lt;/book&gt;&lt;/books&gt;

In file1.php i have the following code that writes the XML to a file :

$file= fopen("book_results.xml", "w");
$xml_writer = new XMLWriter();
$xml_writer->openMemory();
$xml_writer->startDocument('1.0', 'UTF-8', 'yes');
$xml_writer->startElement('books');
$xml_writer->writeElement('date',get_current_date());   // Like December 23rd, 2009
$xml_writer->writeElement('total',$totalResults);   
foreach($bookList as $key => $value) { /* $bookList contains key value pairs */
    $xml_writer->startElement('book');
    $xml_writer->writeElement('name',$key);
    $xml_writer->writeElement('url',$value);
    $xml_writer->endElement(); //book
}
$xml_writer->endElement(); //books
$xml_data = $xml_writer->outputMemory();
fwrite($file,$xml_data);
fclose($file);

And in index.php, i have the following code to send the contents of the file as a response

<?php
    //Send the xml file contents as response
    header('Content-type: text/xml');
    readfile('book_results.xml');
?>

What could be causing the error ?

Please help. Thank You.

A: 

Have you tried using those 2 loose date and total tags as attributes instead?:

<books date="December 24th" total="2">

Also, xml can be quite sensitive. Make sure to use CDATA tags were appropriate

Jeff
Whether date/total are attributes or elements has nothing to do with whether the XML is well-formed. It's a design decision.
Brian Agnew
-1 for even suggesting that CDATA might be useful in this case. The proper use cases for CDATA are so few that you could count them on the fingers of one hand even if you'd been exceptionally careless with a garbage disposal.
Robert Rossney
+1  A: 

There are some free websites and tools for checking for validity in XML.

According to the XML Validator, when I pasted your XML above into the textarea, it said "no errors found".

However, Validome says "Can not find declaration of element 'books'."

Perhaps Jeff's suggestion of changing date and total to attributes might help. It would probably be easy to try that.

DOK
Changed the child elements of book and total to attributes, but still getting the same error :(.
joew
As I've noted above, whether date/total are attributes or elements has nothing to do with whether the XML is well-formed. It's a design decision.
Brian Agnew
+2  A: 

The above looks good to me (including the fact that you're forming the XML via a dedicated component) and either:

  1. what you're using to validate this is wrong
  2. you're looking at something different to what you think you are

I would definitely try another tool/browser/whatever to validate this. Additionally, you may want to save the XML file as sent to the browser, and check it using XMLStarlet (a command-line XML toolkit).

I'm wondering also if it's an issue that we can't easily see - a character encoding problem or a Byte-Order-Mark issue (related to encodings). Does the character encoding of the web page you're sending match/differ from the encoding of the XML (UTF-8).

Brian Agnew
Thanks for the reply Brian. How to check whether the character encoding of the web page im sending matches/differs from the encoding of the XML (UTF-8) ?
joew
Perhaps check the HTTP headers in Firefox using Firebug or LiveHttpHeaders ?
Brian Agnew
+1 For mentioning Byte-Order Mark. If both the PHP and XML file contain them it can confuse the XML parser because a BOM should only appear at the start of the file
Alexandre Jasmin
A: 

It validates fine in WMHelp XMLPad 3.0.1.0, and opens fine in FireFox 3.0.8 and IE7 without errors.

The only thing I can see, from a copy and paste of your XML, is that the XML declaration is followed by a CR/LF combination (0x0D0x0A). This is platform specific (Windows), and may be an issue on the client; you didn't mention what the client was, however, so I can't be sure if that's the problem.

Ken White
A: 

Ensure that you are writing UTF-8 or 7-bit ASCII encoding to the file (test with a text editor or the 'file' command, if you have it), and that your checker supports it. Keep in mind that UTF-8 can include a signature (sometimes called the byte-order mark) in the first three bytes (EF BB BF) that sometimes confuses some tools if it is there, and rarely if it is not.

brianary