提问人:Premlatha 提问时间:1/13/2022 更新时间:1/14/2022 访问量:205
XML 使用 PHP 在 XML 元素中添加编码的 XHTML
xml add encoded xhtml in xml element using php
问:
我想创建嵌入编码的xhtml的xml文件。我单独编码了xhtml文件。在创建xml元素时,我想在xml元素中添加xhtml的编码内容。在我添加并最终输出到浏览器后,浏览器中显示错误。test
此页面包含以下错误: 第 9 行第 144 列的错误:编码错误 下面是页面的渲染,直到第一个错误。
<?php
$dom =new DOMDocument('1.0','utf-8');
$content = (file_get_contents("test_xmlencoding.xhtml"));
$element = $dom->createElement('test', $content);
$dom->appendChild($element);
header('Content-type: text/xml;');
echo $dom->saveXML();
?>
XHTML 文件
<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="TX21_HTM 21.0.406.501" name="GENERATOR" />
<title></title>
</head>
<body style="font-family:'Arial';font-size:12pt;text-align:left;">
<p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"><span style="font-family:'Verdana';font-size:9pt;">ABC1.</span></p>
<p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"><span style="font-family:'Verdana';font-size:9pt;">(ABC2)</span></p>
<p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"><span style="font-family:'Verdana';font-size:9pt;"> </span></p>
<p lang="en-US" style="margin-top:0pt;margin-bottom:0pt;"><span style="font-family:'Verdana';font-size:9pt;">ABC3</span></p>
</body>
</html>
在不编码的情况下添加 xhtml 内容时,输出在浏览器上呈现不会出错。
我已尝试替换
$content = (file_get_contents("test_xmlencoding.xhtml"));
自
$content = htmlentities(file_get_contents("test_xmlencoding.xhtml"));
输出仅显示测试元素的结束标记 。</test>
答:
0赞
ThW
1/14/2022
#1
和属性的第二个参数只有部分转义。他们希望特殊字符已经作为实体进行转义 - 除了 和 。DOMDocument::createElement()
DOMNode::$nodeValue
<
>
$document = new DOMDocument();
$document->appendChild(
$tests = $document->createElement('tests')
);
$tests
->appendChild($document->createElement('test', 'a < b'));
$tests
->appendChild($document->createElement('test', 'a & b'));
echo $document->saveXML();
输出:
Warning: DOMDocument::createElement(): unterminated entity reference b in ... on line 9
<?xml version="1.0"?>
<tests><test>a < b</test><test/></tests>
method 参数不是 DOM 标准的一部分,并且该属性的行为与规范不同。
在原始 DOM 中,您期望将内容添加为单独的文本节点。这也允许混合子节点。现代 DOM 引入了充当快捷方式的属性。DOMNode::$textContent
下面是一个示例:
$xhtml = <<<'XHTML'
<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<em>a & b</em>
</body>
</html>
XHTML;
$document = new DOMDocument();
$document->appendChild(
$tests = $document->createElement('tests')
);
// append child element and set its text content
$tests
->appendChild($document->createElement('test'))
->textContent = $xhtml;
// append child element, then append child text node
$tests
->appendChild($document->createElement('test'))
->appendChild($document->createTextNode($xhtml));
$document->formatOutput = true;
echo $document->saveXML();
输出:
注意双重转义。&amp;
<?xml version="1.0"?>
<tests>
<test><?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<em>a &amp; b</em>
</body>
</html></test>
<test><?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<em>a &amp; b</em>
</body>
</html></test>
</tests>
评论
0赞
Premlatha
1/24/2022
此方法有助于在附加 xml 元素后自动对 xhtml 的实体进行编码。它帮助了我。谢谢。现在,我找到了 xhtml 的解码实体,因为我的源文档是编码的 xhtml 实体。
0赞
ThW
1/24/2022
如果将源代码加载为 HTML,则 DOM 将对命名实体进行解码。3v4l.org/PgeeM#v8.1.2 - 否则它只会解码数字实体。
0赞
Premlatha
1/25/2022
我在加载的 xhtml 内容上使用 utf_decode() 来删除 [它出现在空白处]。然后,我将“\r\n”、“\r”替换为“\n”,因为出现在每行的末尾。 谢谢。Â
$doc>loadHTML($html); $decoded_content=utf8_decode($doc->documentElement->textContent); $normalisdlf_1 =str_replace(array("\r\n", "\r"), "\n", $decoded_content);
0赞
ThW
1/25/2022
与其解码 UTF-8,不如考虑将其用作所有内容的默认编码。 将 UTF-8 转换为拉丁语 1,拉丁语 1 中不可用的任何字符都将丢失/损坏。因此,除了在极少数情况下,例如为其他不理解它的系统导出,请始终使用 utf-8。要规范化文本,请检查 ext/intl 音译器 (stackoverflow.com/a/51602898/497139)。utf8_decode()
0赞
Premlatha
1/26/2022
当我想使用音译器时,它要求在使用前安装 PECL 扩展。我没有继续。我尝试用空字符串替换不间断空格   [以 utf-8 编码:“\xc2\xa0”]。该 Â 不再出现在输出文件中。$removenonbreakingspace_1=str_replace("\xc2\xa0",' ',$xhtml); $doc->loadHTML($removenonbreakingspace_1); $decoded_content_1=$doc->documentElement->textContent; $final=str_replace(array("\r\n", "\r"), "\n", $decoded_content_1);
评论
encoded xhtml
>
<
This page contains the following errors: error on line 8 at column 78: Encoding error Below is a rendering of the page up to the first error.
> <