2015-11-02 37 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