【问题标题】:PHP print xml file in pretty formatPHP以漂亮的格式打印xml文件
【发布时间】:2020-01-24 14:35:34
【问题描述】:

我需要通过 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>";
?>

但是我得到了这个输出:

<pre>SimpleXMLElement Object
(
[@attributes] => Array
    (
        [type] => freeswitch/xml
    )

[section] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [name] => configuration
            )

        [configuration] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [description] => Network Lists
                        [name] => acl.conf
                    )

                [network-lists] => SimpleXMLElement Object
                    (
                        [list] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => localhost_allow
                                                [default] => allow
                                            )

                                        [node] => SimpleXMLElement Object
                                            (
                                                [@attributes] => Array
                                                    (
                                                        [type] => allow
                                                        [cidr] => 127.0.0.1/32
                                                    )
)
</pre>

我只需要在浏览器中输出漂亮的 xml。或者我可以以另一种方式提供 xml 文件。任何想法? 谢谢

【问题讨论】:

    标签: php xml pretty-print


    【解决方案1】:

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

    $file = file_get_contents("test.xml");
    echo $file;
    

    浏览器将为您完成剩下的工作。

    【讨论】:

      【解决方案2】:

      也许这可以解决问题,如果没有,我相信您必须解析 xml,然后您可以根据需要对其进行格式化。

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

      【讨论】:

        猜你喜欢
        • 2011-05-06
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 2010-09-05
        • 2023-03-07
        相关资源
        最近更新 更多