ElementTree in Python 2.6.2 Processing Instructions support?

后端 未结 5 742
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 06:12

I\'m trying to create XML using the ElementTree object structure in python. It all works very well except when it comes to processing instructions. I can create a PI easil

5条回答
  •  长情又很酷
    2020-12-11 06:39

    Yeah, I don't believe it's possible, sorry. ElementTree provides a simpler interface to (non-namespaced) element-centric XML processing than DOM, but the price for that is that it doesn't support the whole XML infoset.

    There is no apparent way to represent the content that lives outside the root element (comments, PIs, the doctype and the XML declaration), and these are also discarded at parse time. (Aside: this appears to include any default attributes specified in the DTD internal subset, which makes ElementTree strictly-speaking a non-compliant XML processor.)

    You can probably work around it by subclassing or monkey-patching the Python native ElementTree implementation's write() method to call _write on your extra PIs before _writeing the _root, but it could be a bit fragile.

    If you need support for the full XML infoset, probably best stick with DOM.

提交回复
热议问题