DOMDocument禁用字符转义

DOMDocument禁用字符转义

本文介绍了DOMDocument禁用字符转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试禁用DOMDocument字符转义.

I am trying to disable DOMDocument character escaping.

所以当从JSON获得像这样的节点时:"something": "<>shouldnotescape"

So when have node from JSON like this one:"something": "<>shouldnotescape"

我希望它在xml中产生完全相同的输出:<something value="<>shouldnotescape"/>相反,我得到:<something value="&lt;&gt;shouldnotescape"/>

I want it to produce the exact same output in xml:<something value="<>shouldnotescape"/>Instead I get: <something value="&lt;&gt;shouldnotescape"/>

我已经尝试关闭了replaceElements和resolveExternals,也尝试了$this->document->saveXML($this->document->documentElement);,但对我没有任何帮助.

I have already tried turning off substituteElements and resolveExternals, also tried $this->document->saveXML($this->document->documentElement); but nothing worked for me.

我没有加载XML,而是遍历JSON树并创建DOMElement.保存之前,每个元素上的值都不会转义,因此,我认为应该可以通过DOMDocument实现.

I am not loading XML, instead, I am traversing the JSON tree and creating DOMElements. The values on each element are non-escaped before saving, so I believe, this should be somehow possible via DOMDocument.

推荐答案

这将是无效的XML.所以这是不可能的.

This would be invalid XML. So it is not possible.

示例:

<?php
$dom = new DOMDocument();
$dom->loadXml('<something value="<>shouldnotescape"/>');

输出:

Warning: DOMDocument::loadXML(): Unescaped '<' not allowed in attributes values in Entity, line: 1 in /tmp/... on line 3
Warning: DOMDocument::loadXML(): attributes construct error in Entity, line: 1 in /tmp/... on line 3

您需要注意,这只是序列化.如果您获取属性的值,则实体将被解码:

You need to be aware that this is only the serialization. If you fetch the value of the attribute the entities will be decoded:

<?php
$dom = new DOMDocument();
$dom->loadXml('<something value="&lt;&gt;shouldnotescape"/>');
var_dump($dom->documentElement->getAttribute('value'));

输出:

string(17) "<>shouldnotescape"

这篇关于DOMDocument禁用字符转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:24