简体   繁体   中英

Figuring out where CDATA is in lxml element?

I need to parse and rebuild a file format used by a parser which speaks a language that can only charitably be described as XML. I realize that standards-compliant XML doesn't care about either the CDATA or the whitespace, but unfortunately this application demands that I care about both...

I'm using lxml.etree because it's pretty good at preserving CDATA.

For example:

s = '''
<root>
  <item>
     <![CDATA[whatever]]>
  </item>
</root>'''

import lxml.etree as et
et.fromstring(s, et.XMLParser(strip_cdata=False))
item = root.find('item')
print et.tostring(item)

This prints:

<item>
    <![CDATA[whatever]]>
  </item>

lxml has exactly preserved the formatting of the <item> tag... great!

The problem is that I don't have any way to tell exactly where the CDATA begins and ends within the text of the tag. The property item.text gives no indication of exactly which part of the text is wrapped in CDATA:

item.text
 ==> '\n     whatever\n  '

So if I modify it, and try to spit it back out as CDATA, then I lose the locations of the whitespace:

item.text = CDATA('foobar')
et.tostring(item)
 ==> '<item><![CDATA[foobar]]></item>\n'

Clearly, lxml "knows" where the CDATA is located within the text of a node, because it preserves it with node.tostring() . However, I can't figure out a way to introspect which parts of the text are CDATA and which aren't. Any advice?

I'm not sure about lxml , but with minidom you can change the CDATA section and preserve the surrounding whitespace, as CDATASection s are a separate node type.

>>> from xml.dom import minidom
>>> data = minidom.parseString(s)
>>> parts = data.getElementsByTagName('item')
>>> item = parts[0]
>>> item.childNodes
[<DOM Text node "u'\n     '">, <DOM CDATASection node "u'whatever'">, <DOM Text node "u'\n  '">]
>>> item.childNodes[1].nodeValue = 'changed'
>>> print item.toxml()
<item>
     <![CDATA[changed]]>
  </item>

See xml.dom.minidom: Getting CDATA values for more details.

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