PHP如何使用具有DOMdocument的XML中的实体 [英] PHP How to use quot; entities in XML with DOMdocument
问题描述
我正在修改其他库生成的XML文件的内容。我正在使用PHP(5.3.10)进行一些DOM修改,然后重新插入替换节点。
I am working on modifying the contents of an XML file generated by some other library. I'm making some DOM modifications with PHP (5.3.10) and reinserting a replacement node.
我正在使用的XML数据具有 & quot
;元素,然后再进行操作,我希望按照 http:// www保留这些元素。 w3.org/TR/REC-xml/ 完成修改后。
The XML data I'm working with has "
; elements before I do the manipulation and I want to keep those elements as per http://www.w3.org/TR/REC-xml/ when I'm done with the modifications.
但是我在更改<$时遇到了PHP问题c $ c>& 元素。参见我的示例。
However I'm having problems with PHP changing the "
elements. See my example.
$temp = 'Hello "XML".';
$doc = new DOMDocument('1.0', 'utf-8');
$newelement = $doc->createElement('description', $temp);
$doc->appendChild($newelement);
echo $doc->saveXML() . PHP_EOL; // shows " instead of element
$node = $doc->getElementsByTagName('description')->item(0);
echo $node->nodeValue . PHP_EOL; // also shows "
输出
<?xml version="1.0" encoding="utf-8"?>
<description>Hello "XML".</description>
Hello "XML".
这是PHP错误还是做错了?我希望不必在每个字符位置都使用createEntityReference。
Is this a PHP error or am I doing something wrong? I hope it isn't necessary to use createEntityReference in every char location.
类似的问题:
PHP XML实体编码问题
编辑:作为示例,显示saveXML不应像&
& 实体>行为正常。这个$ temp字符串应该真正输出,因为它是在saveXML()期间最初与实体一起输入的。
As an example to show saveXML should not be converting the "
entities just like the &
which behaves properly. This $temp string should really be output as it is initially entered with the entities during saveXML().
$temp = 'Hello "XML" &.';
$doc = new DOMDocument('1.0', 'utf-8');
$newelement = $doc->createElement('description', $temp);
$doc->appendChild($newelement);
echo $doc->saveXML() . PHP_EOL; // shows " instead of element like &
$node = $doc->getElementsByTagName('description')->item(0);
echo $node->nodeValue . PHP_EOL; // also shows " &
输出
<?xml version="1.0" encoding="utf-8"?>
<description>Hello "XML" &.</description>
Hello "XML" &.
推荐答案
答案是,它实际上并不需要根据规范进行转义(跳过对CDATA的提及):
The answer is that it doesn't actually need any escaping according to the spec (skipping the mentions of CDATA):
&字符(&)和左尖括号(<)不得以其原义形式出现(... )如果在其他地方需要使用它们,则必须必须使用数字字符引用或字符串
进行转义。 & amp; 和
& lt; "
。右尖括号(>) 可以用字符串表示。 & gt; "
(...)
The ampersand character (&) and the left angle bracket (<) must not appear in their literal form (...) If they are needed elsewhere, they must be escaped using either numeric character references or the strings
" & "
and" < "
respectively. The right angle bracket (>) may be represented using the string" > "
(...)
要允许属性值同时包含单引号和双引号,可以表示单引号或单引号字符(')为 &’ "
,并且双引号字符()为" & quot; 。
To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " ' "
, and the double-quote character (") as " " "
.
您可以使用 createTextNode()$ c $轻松地进行验证c>执行正确的转义:
You can verify this easily by using createTextNode()
to perform the correct escaping:
$dom = new DOMDocument;
$e = $dom->createElement('description');
$content = 'single quote: \', double quote: ", opening tag: <, ampersand: &, closing tag: >';
$t = $dom->createTextNode($content);
$e->appendChild($t);
$dom->appendChild($e);
echo $dom->saveXML();
输出:
<?xml version="1.0"?>
<description>single quote: ', double quote: ", opening tag: <, ampersand: &, closing tag: ></description>
这篇关于PHP如何使用具有DOMdocument的XML中的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!