Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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_Mysql_Xml_Database_Mysqli - Fatal编程技术网

Php 如何正确格式化XML输出

Php 如何正确格式化XML输出,php,mysql,xml,database,mysqli,Php,Mysql,Xml,Database,Mysqli,我正在尝试使用PHP以XML格式显示数据库的内容。我能够显示数据,但无法正确格式化。任何建议都将不胜感激 我正在努力实现的产出 <people> <person> <id>111</id> <first_name>sara</first_name> <last_name>smith</last_name> <email>

我正在尝试使用PHP以XML格式显示数据库的内容。我能够显示数据,但无法正确格式化。任何建议都将不胜感激

我正在努力实现的产出

<people>
    <person>
        <id>111</id>
        <first_name>sara</first_name>
        <last_name>smith</last_name>
        <email>ssmith@mail.com</email >
    </person >
</people>

111
萨拉
史密斯
ssmith@mail.com
输出当前如下所示:

我的PHP代码

<?php 
  // Create con
    $con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");

    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //get student ID from URL
    $STU_ID = $_GET['id'];

    $sql = "SELECT * FROM table";
    $res = mysqli_query($con, $sql);

    $xml = new XMLWriter();

    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);

    $xml->startElement('people');

    while ($row = mysqli_fetch_assoc($res)) {
      $xml->startElement("person");

      $xml->writeElement("id", $row['id']);
      $xml->writeElement("first_name", $row['first_name']);
      $xml->writeElement("last_name", $row['last_name']);
       $xml->writeElement("email", $row['email']);
      $xml->writeRaw($row['person']);

      $xml->endElement();
    }

    $xml->endElement();

    header('Content-type: text/xml');
    $xml->flush();

     mysqli_free_result($res); 
    // Close connections
    mysqli_close($con);
?>

根据上面的评论


在浏览器中查看html或xml时,不会呈现尖括号内的未知标记。您可以使用浏览器的“查看源”选项查看它们

正如Josh Taylor在评论中指出的,您应该能够输出

header('Content-type: text/xml');
但请参见中的注释:

记住,在发送任何实际输出之前,必须调用header(),可以是通过普通HTML标记、文件中的空行,也可以是通过PHP


您当前的输出是什么?添加了当前输出的图像。谢谢阅读格式化版本时,是否需要将其放在其他页面中?或者它总是出现在只有XML的页面上吗?另一种解决方案是放置
标题(“内容类型:text/XML”)在脚本的顶部,告诉浏览器它读取的内容是XML,以便它为您格式化。@JoshTaylor!谢谢,非常感谢。“斜括号内的标签默认隐藏”-不隐藏,但渲染。HTML/XML被呈现为HTML,未知标记被忽略。如果您有一个在HTML中具有可见效果的标记,那么该效果在此处也将可见。