Php XML-创建元素-新行
如何在元素中创建新行 我有: 然而,它增加了Php XML-创建元素-新行,php,xml,dom,domdocument,Php,Xml,Dom,Domdocument,如何在元素中创建新行 我有: 然而,它增加了 到每行的末尾。我怎样才能摆脱它呢是\r\n样式行结尾的回车部分。我认为DOMDocument对它进行编码是为了保存它。如果您检查,它会说如果未编码,它将标准化为\n 所以你有不同的选择: 忽略转义实体,它们将在xml解析器中解码 使用CDATA元素,这里不进行规范化,因此DOMDocument认为无需转义“\r” 确保使用\n样式的行尾保存文件 在创建DOM之前,将行尾规范化为\n 以下是一些显示不同行为的示例源: $text = "test1
代码>到每行的末尾。我怎样才能摆脱它呢
是\r\n
样式行结尾的回车部分。我认为DOMDocument对它进行编码是为了保存它。如果您检查,它会说如果未编码,它将标准化为\n
所以你有不同的选择:
\n
样式的行尾保存文件\n
$text = "test1;\r\ntest2;\r\ntest3;\r\n";
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->appendChild($root = $dom->createElement('root'));
$root->appendChild(
$node = $dom->createElement('code')
);
// text node - CR will get escaped
$node->appendChild($dom->createTextNode($text));
$root->appendChild(
$node = $dom->createElement('code')
);
// cdata - CR will not get escaped
$node->appendChild($dom->createCdataSection($text));
$root->appendChild(
$node = $dom->createElement('code')
);
// text node, CRLF and CR normalized to LF
$node->appendChild(
$dom->createTextNode(
str_replace(array("\r\n", "\r"), "\n", $text)
)
);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<code>test1;
test2;
test3;
</code>
<code><![CDATA[test1;
test2;
test3;
]]></code>
<code>test1;
test2;
test3;
</code>
</root>
嗯,看起来这是一件很难做到的事情:(非常感谢你的解释和所有这些例子:)
<?xml version="1.0" encoding="UTF-8"?>
<root>
<code>test1;
test2;
test3;
</code>
<code><![CDATA[test1;
test2;
test3;
]]></code>
<code>test1;
test2;
test3;
</code>
</root>