微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在浏览器中将数据显示为xml结构

我试图在browser.i中创建一个像xml一样的xml

$query2 = "SELECT * FROM user where user_id = 7";
$dbresult = MysqLi_query($dbcon,$query2) or die('query error...');

$doc = new DomDocument('1.0');
 $doc->formatOutput = true; 

$root = $doc->createElement('root');
$root = $doc->appendChild($root);

while($row = MysqLi_fetch_array($dbresult)) {

  $userId = $doc->createElement('UserId');
  $userId = $root->appendChild($userId);
   $value = $doc->createTextNode($row[0]);
  $value = $userId->appendChild($value);

   $psw = $doc->createElement('Password');
  $psw = $root->appendChild($psw);
   $value = $doc->createTextNode($row[2]);
  $value = $psw->appendChild($value);


} 
echo $doc->saveXML();
$doc->save("test.xml"); 

它只显示数据

7 pwd123

但我想要数据

<xml>
   <root>
     <userId>7</userId>
     <password>pwd123</password>
   </root>
</xml>

该怎么办?
谢谢

解决方法:

你的脚本不是set Content-type headerPHP发送的default content-type header通常是Content-type:text / html.
因此,客户端假定数据是一个html文档,并解析并显示它.
如果客户端/浏览器不“知道”您的文档包含的标签/元素(并且上下文很重要),则适用http://www.w3.org/TR/html401/appendix/notes.html#notes-invalid-docs.

如果你“告诉”客户端输出一个xml文档,它将/可以/应该相应地显示它.

if ( headers_sent() ) {
    // can't set the content-type after output has been sent to the client
    die('error');
}
else {
    header('Content-type: text/xml'); // see http://en.wikipedia.org/wiki/XML_and_MIME
    echo $doc->saveXML();   
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐