【发布时间】:2015-02-03 00:22:43
【问题描述】:
我正在尝试在 HTML 中显示原始 XML。如果没有 HTML 元素,则 XML 显示正常但是一旦我添加我的内容,就会出现此错误
error on line 37 at column 6: XML declaration allowed only at the start of the document
在做了一些研究之后,这个问题很常见,有空格,但我没有注意到。这是我有问题的源代码。
<?php require 'header.php'; ?>
<div class="sixteen columns">
<h3>XML</h3>
<?php
header("Content-Type:text/xml");//Tell browser to expect xml
include("config/init.php");
$connection = mysqli_connect($hostname, $username, $password, $databaseName) or die("you did not connect");
$query = "SELECT * FROM art";
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
//Top of xml file
$_xml = '<?xml version="1.0" encoding="UTF-8"?>';
$_xml .="<art>";
while($row = mysqli_fetch_array($result)) {
$_xml .="<art>";
$_xml .="<art_name>".$row['name']."</art_name>";
$_xml .="<art_category>".$row['category']."</art_category>";
$_xml .="<art_price>".$row['price']."</art_price>";
$_xml .="</art>";
}
$_xml .="</art>";
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
print $xmlobj->asXML();
$xmlobj->asXML('art.xml');
?>
</div>
<?php require 'footer.php'; ?>
http://www.acalvert.x10host.com/xml.php如果你想查看页面源代码
【问题讨论】:
-
您不能在 HTML 文档中更改内容类型。您是在尝试显示 XML 标记 还是尝试使用 XML 标记以便以某种方式呈现 XML 元素?究竟如何? XML 元素没有默认格式(CSS 意义上的内联元素除外)。
-
试图显示 XML 标记,以便它显示在我的页面中。 IE 我可以查看页眉、页脚等