Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php XML解析错误_Php_Xml - Fatal编程技术网

Php XML解析错误

Php XML解析错误,php,xml,Php,Xml,我已经编写了一个PHP脚本来编写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; $res

我已经编写了一个PHP脚本来编写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>

http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
阿夫南
忙碌的
http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
医生
忙碌的
我认为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";

 } ?>
第11列第18行错误:开始和结束标记不匹配:第0页第0行和第1行


您正在使用原始字符串操作创建XML。让打开和关闭标记匹配(每个关闭都有两个打开的
标记
将是直接将SQL结果转义到XML中的许多问题中的第一个


使用一个库,否则您将不得不做大量的工作来修复大量的死角情况。

您正在使用原始字符串操作创建XML。获取匹配的打开和关闭标记(每个关闭都有两个打开的
标记
将是直接将SQL结果转储到XML中的许多问题中的第一个


使用一个库,否则你将不得不做大量的工作来修复大量的角落案例。

删除这行代码
$\u xml.=“\t\r\n”


删除这行代码
$\u xml.=“\t\r\n”


您总共有四个
开始标记,但只有两个
结束标记。

您总共有四个
开始标记,但只有两个
结束标记。

您的输出将无法工作,因为您正在复制
元素。这应该是您的输出(我已经注释了您应该忽略的内容):


http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
阿夫南
忙碌的
http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
医生
忙碌的

您的输出将无法工作,因为您正在复制
元素。这应该是您的输出(我已经注释了您应该忽略的内容):


http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
阿夫南
忙碌的
http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml
家
http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332
医生
忙碌的

正如其他人所说,双页对你不利。此外,虽然它不会给你带来任何问题,但你的整体容器是“atricle”,而不是“article”。

正如其他人所说,双页对你不利。此外,虽然它不会给你带来任何问题,但你的整体容器是“atricle”,而不是“article”

while ($row = mysql_fetch_array($result)) {
<?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>