Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents() and XML files [duplicate]

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>

If I use

$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.

like image 589
Marek Hajžman Avatar asked Dec 21 '22 05:12

Marek Hajžman


1 Answers

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

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);

that should do the trick

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>';
like image 55
Ph.T Avatar answered Dec 28 '22 07:12

Ph.T