Figuring out where CDATA is in lxml element?

自古美人都是妖i 提交于 2019-12-22 11:18:12

问题


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?


回答1:


I'm not sure about lxml, but with minidom you can change the CDATA section and preserve the surrounding whitespace, as CDATASections 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.



来源:https://stackoverflow.com/questions/25171791/figuring-out-where-cdata-is-in-lxml-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!