简体   繁体   中英

Python LXML: Modify 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?

Given the following results:

>>> 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>'

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>'
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM