ElementTree in Python 2.6.2 Processing Instructions support?

后端 未结 5 692
隐瞒了意图╮
隐瞒了意图╮ 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:14

    With the lxml API it couldn't be easier, though it is a bit "underdocumented":

    If you need a top-level processing instruction, create it like this:

    from lxml import etree
    
    root = etree.Element("anytagname")
    root.addprevious(etree.ProcessingInstruction("anypi", "anypicontent"))
    

    The resulting document will look like this:

    
    
    

    They certainly should add this to their FAQ because IMO it is another feature that sets this fine API apart.

提交回复
热议问题