How do I create an xml document in python

后端 未结 2 1818
情歌与酒
情歌与酒 2021-02-07 03:53

Here is my sample code:

from xml.dom.minidom import *
def make_xml():
    doc = Document()
    node = doc.createElement(\'foo\')
    node.innerText = \'bar\'
            


        
2条回答
  •  误落风尘
    2021-02-07 04:39

    @Daniel

    Thanks for the reply, I also figured out how to do it with the minidom (I'm not sure of the difference between the ElementTree vs the minidom)

    
    from xml.dom.minidom import *
    def make_xml():
        doc = Document();
        node = doc.createElement('foo')
        node.appendChild(doc.createTextNode('bar'))
        doc.appendChild(node)
        return doc
    if __name__ == '__main__':
        make_xml().writexml(sys.stdout)
    
    

    I swear I tried this before posting my question...

提交回复
热议问题