dqx13503925528 2014-12-04 22:43
浏览 178
已采纳

在HTML中显示XML

I'm trying to display raw XML within HTML. If there's no HTML elements the XML displays fine However soon as I add my content, I am given this error

error on line 37 at column 6: XML declaration allowed only at the start of the document

After doing some research, the problem is common with having white spaces but I cant notice any. Heres my source code in question.

<?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 If you wish to view page source

  • 写回答

3条回答 默认 最新

  • doumu1212 2014-12-04 23:41
    关注

    If you want markup to be displayed then change all < to &lt; and > to &gt;.

    Before displaying anything load your XML to string variable and use this:

    $before = array('<','>');
    $after = array('&lt;','&gt;');
    $xml = str_replace($before,$after,$xml);
    echo $xml;
    

    Secondly - you cannot call header() function after displaying ANY character. Don't use it at all. You want default text/html in your document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?