[英]lxml.etree not working with cdata in python 3
I want to change a value in XML file to CDATA with LXML. 我想使用LXML将XML文件中的值更改为CDATA。
It works perfectly when I simply change the text, but when CDATA is used, the content of the element is not replaced. 当我仅更改文本时,它完美地工作,但是当使用CDATA时,不替换元素的内容。
This is how I do the CDATA hack: https://blog.ionelmc.ro/2014/06/15/lxml-element-builder-and-cdata/ 这是我做CDATA骇客的方式: https : //blog.ionelmc.ro/2014/06/15/lxml-element-builder-and-cdata/
This is how I change a text value (between "RESPONSE" tags in XML shown below): Change text value with lxml 这就是我更改文本值的方式(如下所示,在XML中的“ RESPONSE”标签之间): 用lxml更改文本值
Question : How is it possible to change the content text of the RESPONSE tags to CDATA? 问题 :如何将RESPONSE标签的内容文本更改为CDATA?
from lxml import etree
from lxml.builder import ElementMaker
from lxml.etree import CDATA
def add_cdata(element, cdata):
assert not element.text, "Can't add a CDATA section. Element already has some text: %r" % element.text
element.text = cdata
E = ElementMaker(typemap={
CDATA: add_cdata
})
print("\nThe CDATA is working here perfectly: ")
print(etree.tostring(E.RESPONSE(CDATA('Some stuff that needs to be in a CDATA section'))))
tree = etree.fromstring('''<REQRES_MAPPING>
<REQUEST>aaa</REQUEST>
<RESPONSE>bbb</RESPONSE>
</REQRES_MAPPING>''')
print("\nThe data I need to change: ")
print(etree.tostring(tree))
response = tree.xpath("//RESPONSE")
if response:
response[0].text = 'xxx' # this is working, but I need CDATA
print("\nThe text has changed between the RESPONSE tags: ")
print(etree.tostring(tree))
if response:
response[0] = E.RESPONSE(CDATA('xxx')) # this is not working
print("\nThis is not working here: ")
print(etree.tostring(tree))
What I want to get: 我想得到什么:
<REQRES_MAPPING>
<REQUEST>aaa</REQUEST>
<RESPONSE><![CDATA[yyy]]</RESPONSE>
</REQRES_MAPPING>
Obviously it's not a good method to insert the yyy with CDATA decoration, because at the end the LMXL will change CDATA's '<' and '>' tags to <
显然,将yyy插入CDATA装饰不是一个好方法,因为LMXL最后会将CDATA的'<'和'>'标记更改为
<
;。 and >
和
>
. 。
By doing response[0] = ...
you're just modyfying the list named response
, you're not actually touching the tree
at all. 通过执行
response[0] = ...
您只是修改名为response
的列表,实际上根本没有触及tree
。
You need: 你需要:
tree.xpath("//RESPONSE")[0].text = CDATA('xxx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.