Php 如何在浏览器中以xml结构显示数据

Php 如何在浏览器中以xml结构显示数据,php,xml,Php,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

我正在尝试在浏览器中显示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"); 
它显示的数据就像

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

但我需要像这样的数据

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

7.
pwd123
怎么办?
谢谢

使用右键单击->显示源代码命令

您的脚本不会显示,php发送的通常是
内容类型:text/html

因此,客户端假定数据是html文档,并将其解析和显示为html文档。
如果客户机/浏览器不“知道”文档包含的标记/元素(以及上下文事项),则应用该标记/元素

如果您“告诉”客户机输出是一个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();   
}

更改标题内容类型text/xml

header('Content-type: text/xml');
$doc->saveXML();
有关更多详细信息,请参阅