2011-05-10 131 views
0

我有xml文件(product.xml),我需要在php程序中显示此文件的全部内容(包括xml声明,标签和节点值)。我怎么能做到这一点?在php中显示xml文件内容

products.xml

<?xml version="1.0" encoding="utf-8"?> 
<products> 
<product onshelf="yes">   
<type>cosmetic</type>   
<address> 
<suburb>Melbourne</suburb> 
</address>  
</product> 
<product onshelf="yes">   
<type>cosmetic</type>   
<address> 
<suburb>Melbourne</suburb> 
</address>  
</product> 
</products> 

输出:

<?xml version="1.0" encoding="utf-8"?> 
<products> 
<product onshelf="yes">   
<type>cosmetic</type>   
<address> 
<suburb>Melbourne</suburb> 
</address>  
</product> 
<product onshelf="yes">   
<type>cosmetic</type>   
<address> 
<suburb>Melbourne</suburb> 
</address>  
</product> 
</products> 

回答

1
<?php echo htmlspecialchars(file_get_contents('product.xml')); ?> 

的问题是有点误导。正确形式化: 如何使用PHP在浏览器中显示原始XML。

+0

51个答案之后,你应该知道你需要将代码缩进4个空格...... P – deceze 2011-05-10 02:12:40

+0

@deceze实际上,当我使用单行代码结束反引号时, :P – Mel 2011-05-10 02:14:28

+0

它只显示节点值,但不显示xml decelaration和tags.i表示格式与存储在“product.xml”文件中相同 – sara 2011-05-10 02:20:46