Python XML:编写 "而不是 &quot [英] Python XML: write " instead of &quot

查看:61
本文介绍了Python XML:编写 "而不是 &quot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 的 xml minidom 并且一切正常,除了在文本序列中它写出 &quot 转义字符而不是 ".这当然有意义,如果引用出现在标签中,但它在文本中引起了我的注意.我该如何更改?

I am using Python's xml minidom and all works well except that in text sequences it writes out &quot escape characters instead of ". This of course makes sense if a quote appears in a tag, but it bugs me in the text. How do I change this?

推荐答案

查看源代码(Python 3.2,如果重要的话),这是在 _write_data() 函数中硬编码的.您需要修改 TextNode 的 writexml() 方法 - 通过将其子类化或简单地编辑它 - 以便它不调用该方法,而是执行类似于仅转义 < 的操作.和 >.

looking at the source (Python 3.2 if it matters), this is hardcoded in the _write_data() function. you would need to modify the writexml() method of TextNode - either by subclassing it or simply editing it - so that it didn't call that method, but instead did something similar to escape only < and >.

如果您在包之外创建了一个子类(而不是复制和破解包来制作您自己的自定义 xmlminidom),那么看起来,只要稍加注意,您就可以使事情发挥作用.因此,您将创建自己的(子类)TextNode,修改如上,然后,要向 DOM 添加文本,您将添加新类的实例(或用该类的实例替换现有文本节点).您需要设置 ownerDocument 属性.也许最简单的方法是将 Document 子类化并修复 createTextNode() 方法.

if you created a subclass outside of the package (instead of copying and hacking the package to make your own custom xmlminidom) then it looks like, with a little care, you could make things work. so you would create your own (subclass of) TextNode, modified as above and then, to add text to the DOM, you would add an instance of your new class (or replace existing text nodes with instances of that class). you would need to set the ownerDocument attribute. perhaps simplest would be to also subclass Document and fix the createTextNode() method.

但我没有看到更简单的方法来做你想做的事.最好使用更好的 dom 实现.

but i don't see a simpler way of doing what you want. it might be best to use a better dom implementation.

ps 我不知道 xml 规范是否需要这种行为.更新:快速浏览 http://www.w3.org/TR/2008/REC-xml-20081126/#syntax 表明只有 <和 &必须被编码.

ps i have no idea whether this behaviour is required by the xml spec, or not. update: a quick scan of http://www.w3.org/TR/2008/REC-xml-20081126/#syntax suggests that only < and & must be encoded.

这篇关于Python XML:编写 &quot;而不是 &amp;quot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆