- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在修改由其他库生成的 XML 文件的内容。我正在使用 PHP (5.3.10) 进行一些 DOM 修改并重新插入替换节点。
我正在处理的 XML 数据有 "
;在我进行操作之前的元素,我想按照 http://www.w3.org/TR/REC-xml/ 保留这些元素当我完成修改时。
但是我在使用 PHP 更改 "
元素时遇到了问题。请参阅我的示例。
$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。
类似问题: PHP XML Entity Encoding issue
编辑:作为显示 saveXML 的示例,不应像行为正常的 &
那样转换 "
实体。这个 $temp 字符串应该真正输出,因为它最初是在 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" &.
最佳答案
答案是根据 spec 它实际上不需要任何转义(跳过 CDATA 的提及):
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" > "
(...)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()
执行正确的转义来轻松验证这一点:
$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 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399653/
我有 2 个“DOMDocument”对象 - $original 和 $additional。我想要的是从 $additional DOMDocument 中获取所有子级并将其附加到 $origin
我有 2 个“DOMDocument”对象 - $original 和 $additional。我想要的是从 $additional DOMDocument 中获取所有子级并将其附加到 $origin
我有一个代码可以将 XML 文件保存到我的目录中。它在我的本地主机和我的共享主机中实际上就像一个魅力,但它在我的 Linux VPS 中不起作用。 我总是遇到这个错误: 警告:DOMDocument:
我试图从某些网页中获取“链接”元素。我无法弄清楚我做错了什么。我收到以下错误: Severity: Warning Message: DOMDocument::loadHTML() [domdocum
有什么区别: Msxml2.DOMDocument Msxml2.XMLHTTP ?当然,另一个问题是哪一个最适合我的目的,如下所述? 上下文是这样的 - 我有代码可以多次调用来检索网页。我正在寻找执
安装后 Windows Server 2012 和 Windows Server 2016 原生支持哪些版本的 MSXML 和 DOMDocument? 最佳答案 Modern versions of
安装后 Windows Server 2012 和 Windows Server 2016 原生支持哪些版本的 MSXML 和 DOMDocument? 最佳答案 Modern versions of
我正在使用以下代码: $doc = new DOMDocument(); $doc->loadHTML("From: fsong | #001I hate you DomDocument :(.you
我使用 xml、xsl 截取服务器的响应并提取所需的片段,以根据客户端请求从服务器响应中提取 html 片段。例如,假设 $content 在我们处理它之前有服务器响应。 $dom = new
我之前在 RapidXml 中询问过一个类似的问题,我现在想知道,相同但使用 Xerces-C。 我正在开发一个需要解析 xml 的 C++ 应用程序。 考虑以下几点: xml文件:file1.xml
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can I get an element's serialised HTML with PHP's
我有以下 test.php文件,当我运行它时,关闭 标签被删除。 loadHTML(' console.log("hello");
$dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML($content); $divs = $dom->getElementsByTagName
获得除png扩展名以外的所有图像? $xpath = new DOMXPath( $htmlget); $nodelist = $xpath->query("//img[!ends-wi
我想删除所有 script 元素以及此处的代码 aaa EOF; $dom = new DOMDocument(); $dom->loadHTML($pageFile); foreach (
我想制作一个函数,向给定 html 的根标签添加一些属性。 我正在这样做: $dom = new \DOMDocument(); $dom->loadHTML($content);
我想制作向给定 html 的根标记添加一些属性的函数。 我这样做: $dom = new \DOMDocument(); $dom->loadHTML($content); $
我想做的是从 body 标签中获取脚本,但只有包含文本而不是脚本链接的脚本 例如。 console.log("for a test run"); 不是具有文件 src 的脚本。 我想将这些脚本放在页尾
我正在使用 domDocument 来解析这个小的 html 代码。我正在寻找具有特定 id 的特定 span 标签。 Hello world 我的代码: $dom = new domDocument
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是一名优秀的程序员,十分优秀!