PHP以漂亮的格式打印xml文件

PHP以漂亮的格式打印xml文件,php,xml,pretty-print,Php,Xml,Pretty Print,我需要通过Apache请求在浏览器中显示一个XML文件。这样我就可以把文件交给freeswitch了。我的PHP代码如下所示: <?php header('Content-Type: text/xml'); $xml=simplexml_load_file("test.xml") or die("not found"); echo "<pre>".print_r($xml,true)."</pre>"; ?> <?php header('Co

我需要通过Apache请求在浏览器中显示一个XML文件。这样我就可以把文件交给freeswitch了。我的PHP代码如下所示:

<?php
header('Content-Type: text/xml');
$xml=simplexml_load_file("test.xml") or die("not found");
echo "<pre>".print_r($xml,true)."</pre>";
?>
 <?php
    header('Content-Type: text/xml');
    $xml=simplexml_load_file("test.xml") or die("not found");
    echo $xml->asXML();
 ?>

但我得到的结果是:

simplexmlement对象
(
[@attributes]=>数组
(
[type]=>freeswitch/xml
)
[部分]=>SimpleXMLElement对象
(
[@attributes]=>数组
(
[名称]=>配置
)
[配置]=>SimpleXMLElement对象
(
[@attributes]=>数组
(
[说明]=>网络列表
[名称]=>acl.conf
)
[网络列表]=>SimpleXMLElement对象
(
[列表]=>数组
(
[0]=>SimpleXMLElement对象
(
[@attributes]=>数组
(
[名称]=>本地主机\u允许
[默认值]=>允许
)
[node]=>SimpleXMLElement对象
(
[@attributes]=>数组
(
[类型]=>允许
[cidr]=>127.0.0.1/32
)
)
我只需要在浏览器中输出一个漂亮的xml。或者我可以用另一种方式提供xml文件。你知道吗?
谢谢

也许这会奏效,如果不行,我相信您必须解析xml,然后您可以按照自己的意愿格式化它


也许这样就可以了,如果不行,我相信您必须解析xml,然后您可以按照自己的意愿格式化它


您不需要将XML文件作为对象加载。 只需加载文件的原始内容并打印即可:


浏览器将为您完成其余工作。

您不需要将XML文件作为对象加载。 只需加载文件的原始内容并打印即可:

浏览器将为您完成其余的工作

 <?php
    header('Content-Type: text/xml');
    $xml=simplexml_load_file("test.xml") or die("not found");
    echo $xml->asXML();
 ?>
$file = file_get_contents("test.xml");
echo $file;