我根据用户输入创建XML。其中一个xml节点有一个CDATA部分。如果插入CDATA部分的其中一个字符是'特殊'(我认为是控制字符),则整个xml变为无效。
示例:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->appendChild($dom->createElement('root'))
->appendChild($dom->createCDATASection(
"This is some text with a SOH char \x01."
));
$test = new DOMDocument;
$test->loadXml($dom->saveXML());
echo $test->saveXml();
将给出
Warning: DOMDocument::loadXML(): CData section not finished
This is some text with a SOH cha in Entity, line: 2 in /newfile.php on line 17
Warning: DOMDocument::loadXML(): PCDATA invalid Char value 1 in Entity, line: 2 in /newfile.php on line 17
Warning: DOMDocument::loadXML(): Sequence ']]>' not allowed in content in Entity, line: 2 in /newfile.php on line 17
Warning: DOMDocument::loadXML(): Sequence ']]>' not allowed in content in Entity, line: 2 in /newfile.php on line 17
Warning: DOMDocument::loadXML(): internal errorExtra content at the end of the document in Entity, line: 2 in /newfile.php on line 17
<?xml version="1.0"?>
有没有一种好的方法在php中确保CDATA部分有效?
答案 0 :(得分:8)
range of characters允许的CDATA section是
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
因此,您必须清理字符串以仅包含这些字符。
答案 1 :(得分:2)
因为“\ x01”不是可打印字符。所以引起警告。您可以像这样解决这个问题:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->appendChild($dom->createElement('root'))
->appendChild($dom->createCDATASection(
urlencode("This is some text with a SOH char \x01.")
));
$test = new DOMDocument;
$test->loadXml($dom->saveXML());
echo urldecode($test->saveXml());
答案 2 :(得分:1)
使用戈登的答案,我做了:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Platform rope -->
<div class="rope_1">
</div>
<div class="rope_2">
</div>
<div class="rope_3">
</div>
<div class="rope_4">
</div>
使用类似:
答案 3 :(得分:-1)
查看simplexml_load_file
(http://php.net/manual/en/function.simplexml-load-file.php)LIBXML_NOCDATA
选项(http://www.php.net/manual/en/libxml.constants.php)。这很可能会回答你的问题。