2015-11-02 38 views
0

我有一个简单的xml字符串,我用php curl得到,我需要在我的页面中显示它。 的问题是当我使用一个线XML字符串显示:在html中显示简单的xml美化用php

$result = curl_exec($ch); 
echo '<pre>', htmlentities($result), '</pre>'; 

我不希望它在一个大线,我想它断行美化。 我需要动态的东西,因为我得到许多不同的XML格式。 因此,例如,如果我得到这个XML:

<?xml version="1.0"?><response success="1" time="2015-11-02 08:18:42" session="1234"><book status="ok"><server>http://my-server/</server></book></response> 

,我需要这样显示的:

<?xml version="1.0"?> 
    <response success="1" time="2015-11-02 08:18:42" session="1234"> 
     <book status="ok"> 
      <server>http://my-server/</server> 
     </book> 
    </response> 

请帮助我。

+1

http://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php – Nirnae

+0

我不需要将xml转换为数组 – tizmantiz

回答

0

我用DOMDocument()分析和格式化像这样的XML $string

$dom = new DOMDocument(); 
$dom->preserveWhiteSpace = FALSE; 
$dom->loadXML($string); 
$dom->formatOutput = TRUE; //this is the key to formatting.. 
return $dom->saveXML(); 
//echo $dom->saveXML(); 
+0

你好,我试过了,我有空白的白屏。 – tizmantiz

+0

尝试评论'返回'行,并取消注释'回声'行 – MaggsWeb

+0

这就是我做的,但我得到了空白的白色屏幕。 – tizmantiz