2010-12-18 156 views
1

我写了一个PHP脚本编写的XML文档,但有一个错误:XML解析错误

<?php 
$db = "unadb"; 
$connection = mysql_connect("localhost", "root", "") or die("Could not connect."); 
$table_name = 'article'; 

$db = mysql_select_db($db, $connection); 
$query = "select * from " . $table_name; 
$result = mysql_query($query, $connection) or die("Could not complete database query"); 
$num = mysql_num_rows($result); 
if ($num != 0) { 
$file= fopen("results.xml", "w"); 
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n"; 
$_xml .="<atricle>"; 

while ($row = mysql_fetch_array($result)) { 
     $_xml .="\t<page>\r\n"; 
if ($row["title"]) { 
    $_xml .="\t<page title=\"" . $row["title"] . "\">\r\n"; 
    $_xml .="\t<source>" . $row["source"] . "</source>\r\n"; 
    $_xml .="\t<kw>" . $row["kw"] . "</kw>\r\n"; 
    $_xml .="\t\t<Url>" . $row["url"] . "</Url>\r\n"; 
    $_xml .="\t\t<author>" . $row["author"] . "</author>\r\n"; 
    $_xml .="\t<status>" . $row["status"] . "</status>\r\n"; 
    $_xml .="\t</page>\r\n"; 
} else { 
$_xml .="\t<page title=\"Nothing Returned\">\r\n"; 
$_xml .="\t\t<file>none</file>\r\n"; 
$_xml .="\t</page>\r\n"; 
} } 
$_xml .="</atricle>"; 

fwrite($file, $_xml); 
fclose($file); 

echo "XML has been written. <a href=\"results.xml\">View the XML.</a>"; 
} else { 
echo "No Records found"; 

} ?> 

输出是:

<?xml version="1.0" encoding="UTF-8" ?> 
<atricle> <page> 
    <page title="Coming Race"> 
    <source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source> 
    <kw>home</kw> 
     <Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url> 
     <author>afnan</author> 
    <status>active</status> 
    </page> 
    <page> 
    <page title="Coming Race"> 
    <source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source> 
    <kw>home</kw> 
     <Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url> 
     <author>Doctor</author> 
    <status>active</status> 
    </page> 
</atricle> 

我认为XML是确定的,但有错误:

error on line 18 at column 11: Opening and ending tag mismatch: page line 0 and atricle

回答

3

您正在使用原始字符串操作创建XML。获取开启和关闭标签以匹配(每个关闭有两个打开的<page>标签</page>将成为转义直接进入XML的SQL结果的许多问题中的第一个

使用库,或者你将不得不做了非常大量的工作来解决大量的边界情况。

2

删除此行的代码$_xml .="\t<page>\r\n";

while ($row = mysql_fetch_array($result)) { 
     $_xml .="\t<page>\r\n"; 

while ($row = mysql_fetch_array($result)) { 
0

您总共有四个<page>开标签,但只有两个</page>关闭标签。

0

由于您复制了<page>元素,您的输出将不起作用。这应该是你输出(I发表过评论,你应该忽略什么):

<?xml version="1.0" encoding="UTF-8" ?> 
<atricle> 
    <!-- <page> -->  
    <page title="Coming Race"> 
    <source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source> 
    <kw>home</kw> 
     <Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url> 
     <author>afnan</author> 
    <status>active</status> 
    </page> 
    <!-- <page> -->  
    <page title="Coming Race"> 
    <source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source> 
    <kw>home</kw> 
     <Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url> 
     <author>Doctor</author> 
    <status>active</status> 
    </page> 
</atricle> 
0

正如其他人所说,双页都在做你另外,虽然它不给你造成任何问题,你的整体容器是“atricle”而不是“article”。