簡體   English   中英

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

[英]PHP print xml file in pretty format

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

但我得到了這個 output:

<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 output 在瀏覽器中。 或者我可以用另一種方式提供 xml 文件。 任何想法? 謝謝

您不需要將 XML 文件加載為 object。 只需加載文件的原始內容並打印它:

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

瀏覽器將為您執行 rest。

也許這可以解決問題,如果沒有,我相信您必須解析 xml 然后您可以根據需要對其進行格式化。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM