如何使用 php 在浏览器中读取 xml 文件?

How to read xml file as it is in browser using php?

我使用 DOMDocument 创建了一个 xml 文件。我想阅读浏览器中的 xml 文件。如果我这样做

$xml = simplexml_load_file('/var/lol/test.xml');
print_r($xml);

它显示解析的对象而不是 xml 本身。我希望 xml 按原样显示。我该怎么做呢。我也试过file_get_contents()。不起作用。请帮忙。

使用下面的header来显示XML:

header("Content-type: text/xml");
echo file_get_contents("/path/to/xml/file.xml");

使用如下方式显示 xml 网络上的内容。

echo '<p>This is XML string content:</p>';
echo '<pre>';
echo htmlspecialchars(file_get_contents("test.xml"));
echo '</pre>';

test.xml内容如下

<?xml version="1.0"?>
<book>
  <title>This is the title</title>
</book>