PHP file_get_contents()和XML文件 [英] PHP file_get_contents() and XML files

查看:118
本文介绍了PHP file_get_contents()和XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我想加载XML文件(作为文本文件)并在屏幕上显示其内容(作为文本).我有一个

in PHP I want to load a XML file (as a text file) and show its content (as a text) on a screen. I have a simple XML in the form

<root> <parent>Parent text. </parent></root>

如果我使用

$myxmlfilecontent = file_get_contents('./myfile.xml');
echo $myfilecontent; 

仅打印节点父"的内容,仅打印父文本",而不是整个文件内容.

prints only the content of the node "parent", it prints only "Parent text.", not the whole file content.

推荐答案

当您在HTML页面中打印XML时,该XML被吸收为HTML,因此看不到标记.

When you print XML in an HTML page, the XML is assimilated to HTML, so you do not see the tags.

要将标签视为文本,应将其替换为HTML对应实体:

To see the tags as text, you should replace them with the HTML corresponding entity:

$myxmlfilecontent = file_get_contents('./myfile.xml');
echo str_replace('<', '&lt;', $myxmlfilecontent);

应该可以解决的问题

我建议您也将xml括在"pre"中,以保留表示空间

I recommend you to also enclose the xml into a 'pre' to preserve spaces for presentation

$myxmlfilecontent = file_get_contents('./myfile.xml');
echo '<pre>' . str_replace('<', '&lt;', $myxmlfilecontent) . '</pre>';

这篇关于PHP file_get_contents()和XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆