如何在PHP中以HTML格式显示XML?
我有一个XML的string:
$string = " <shoes> <shoe> <shouename>Shoue</shouename> </shoe> </shoes> "; 并希望像这样显示在我的网站上:
 This is XML string content: <shoes> <shoe> <shouename>Shoue</shouename> </shoe> </shoes> 
所以我想这样做:
- 现场,而不是在文本框中
- 没有外部图书馆,框架等
- 用适当的新行格式化
- 使用标签格式化
- 没有颜色等,只有文字
那么如何以简单明了的方式来做到这一点呢?
 如果您只想要(预格式化)string的纯文本表示,可以将其包装在HTML <pre/>标记中,并使用htmlentities来转义尖括号: 
 <?PHP echo '<pre>', htmlentities($string), '</pre>'; ?> 
 你可以使用htmlentities() , htmlspecialchars()或者类似的函数。 
它应该像这样工作:
 echo '<p>This is XML string content:</p>' echo '<pre>'; echo htmlspecialchars($string); echo '</pre>'; 
如果它是一个SimpleXMLobject
 <pre> <?php echo htmlspecialchars(print_r($obj,true)); ?> </pre> 
我search了一个解决scheme,有一点点彩色输出:
 $escaped = htmlentities($content); $formatted = str_replace('<', '<span style="color:blue"><', $escaped); $formatted = str_replace('>', '></span>', $formatted); echo "<pre>$formatted</pre>\n";