简体   繁体   English

XML格式错误

[英]XML not well formed error

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. 我有一个php脚本将xml数据写入文件,另一个脚本将此文件的内容作为响应发送到客户端。 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解析错误格式不正确当我查看页面的来源时,我看到的XML如下:

<?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</url></book><book><name>Book 2</name><url>http://www.anotherdomain.com/posters/76198/img1.jpg</url></book></books>

In file1.php i have the following code that writes the XML to a file : 在file1.php中,我有以下代码将XML写入文件:

$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 在index.php中,我有以下代码将文件的内容作为响应发送

<?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. 谢谢。

The above looks good to me (including the fact that you're forming the XML via a dedicated component) and either: 上面看起来对我很好(包括你通过专用组件形成XML的事实),并且:

  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). 此外,您可能希望将XML文件保存为发送到浏览器,并使用XMLStarlet (命令行XML工具包)进行检查。

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). 您发送的网页的字符编码是否与XML的编码(UTF-8)不匹配/不同。

There are some free websites and tools for checking for validity in XML. 有一些免费的网站和工具可用于检查XML的有效性。

According to the XML Validator , when I pasted your XML above into the textarea, it said "no errors found". 根据XML Validator ,当我将上面的XML粘贴到textarea时,它说“没有发现错误”。

However, Validome says "Can not find declaration of element 'books'." 然而, Validome说“无法找到元素'书籍'的声明。”

Perhaps Jeff's suggestion of changing date and total to attributes might help. 也许杰夫建议将datetotal更改为属性可能会有所帮助。 It would probably be easy to try that. 尝试这可能很容易。

xml version='1.0' encoding='UTF-8' standalone='yes' xml version ='1.0'coding ='UTF-8'standalone ='yes'

use single quote. 使用单引号。

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. 此外,xml可能非常敏感。 Make sure to use CDATA tags were appropriate 确保使用CDATA标签是合适的

It validates fine in WMHelp XMLPad 3.0.1.0, and opens fine in FireFox 3.0.8 and IE7 without errors. 它在WMHelp XMLPad 3.0.1.0中验证正常,并在FireFox 3.0.8和IE7中正常打开而没有错误。

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). 从XML的复制和粘贴中我唯一能看到的是XML声明之后是CR / LF组合(0x0D0x0A)。 This is platform specific (Windows), and may be an issue on the client; 这是特定于平台的(Windows),可能是客户端的问题; you didn't mention what the client was, however, so I can't be sure if that's the problem. 但是,你没有提到客户端是什么,所以我不能确定这是不是问题。

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. 确保您正在为文件写入UTF-8或7位ASCII编码(使用文本编辑器或“file”命令进行测试,如果有的话),并且检查器支持它。 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. 请记住,UTF-8可以在前三个字节(EF BB BF)中包含一个签名(有时称为字节顺序标记),如果它存在,有时会混淆某些工具,如果不存在则很少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM