Python LXML:修改CDATA [英] Python LXML: Modify CDATA

查看:67
本文介绍了Python LXML:修改CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我处理使用Python LXML模块(特别是 lxml.objectify ,但我认为没有什么不同)解析的XML时,如何保存CDATA?

When I'm manipulating XML parsed with the Python LXML module (specifically lxml.objectify, but I don't think it makes a difference), how can I preserve CDATA?

给出以下结果:

>>> from lxml import objectify, etree
>>> xml = '''
  <Root>
   <Child>
    <![CDATA[abcd]]>
   </Child>
  </Root>
  '''
>>> parser = objectify.makeparser(strip_cdata=False)
>>> parsed = objectify.XML(xml, parser=parser)
>>> etree.tostring(parsed)
'<Root><Child><![CDATA[abcd]]></Child></Root>'
>>> type(parsed.Child)
<type 'lxml.objectify.StringElement'>
>>> parsed.Child.text
'abcd'
>>> parsed.Child = 'efgh'
>>> etree.tostring(parsed)
'<Root><Child xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">efgh</Child></Root>'

我希望最后一行仍然具有<![CDATA [....]> .但是我看不到任何保存或重新创建它的方法.尝试访问< Child> 元素的内容会产生一个空字符串,并且修改该元素的内容会神奇地消失CDATA部分.

I'd like that last line to still have the <![CDATA[....]>. But I can't see any way of either preserving it or recreating it. Attempts to access the content of the <Child> element produce a bare string, and modifying the content of that element magically disappears the CDATA section.

做这件事的正确方法是什么?

What's the right way of doing this?

推荐答案

>>> from lxml import etree
>>> parser = etree.XMLParser(strip_cdata=False)
>>> parsed = etree.XML('''
...   <Root>
...    <Child>
...     <![CDATA[abcd]]>
...    </Child>
...   </Root>
... ''', parser)
>>> print etree.tostring(parsed)
<Root>
   <Child>
    <![CDATA[abcd]]>
   </Child>
  </Root>
>>> parsed.getchildren()[0].text = etree.CDATA('efgh updated')
>>> etree.tostring(parsed)
'<Root>\n   <Child><![CDATA[efgh updated]]></Child>\n  </Root>'
>>>

这篇关于Python LXML:修改CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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