我正在从XML文件中读取内容以显示网页内容,请参阅读取XML文件的代码片段,
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$arg = $_GET['content'];
//echo $arg;
$doc = new DOMDocument();
$doc->load('History.XML');
$contents = $doc->getElementsByTagName($arg);
$content1 = $contents->item(0)->nodeValue;
$content2 = $contents->item(1)->nodeValue;
?>
<p>
<?php
echo <<<EOM
$content1
EOM;
?>
</p>
</body>
</html>
XML文件内容有一些HTML格式化标签,如<b>
<h3>
等,但从XML读取和显示这些内容,HTML格式化没有发生。
nodeValue
在您的情况下是所有文本节点的连接。
使用字符串DOMDocument::saveXML ([DOMNode $node [, int $options]])代替。
自包含的例子:
<?php
$arg = 'foo';
$doc = new DOMDocument();
$doc->loadxml('<x>
<foo><h1>lalala</h1><b>xyz</b></foo>
<foo>12345</foo>
</x>');
$contents = $doc->getElementsByTagName($arg);
$content1 = $contents->item(0);
$content2 = $contents->item(1);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<p><?php echo $doc->savexml($content1); ?></p>
</body>
</html>
打印
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<p><foo><h1>lalala</h1><b>xyz</b></foo></p>
</body>
</html>