Generating xml in python and lxml

前端 未结 4 998
逝去的感伤
逝去的感伤 2020-12-25 08:21

I have this xml from sql, and I want to do the same by python 2.7 and lxml



  

        
4条回答
  •  眼角桃花
    2020-12-25 08:56

    from lxml import etree
    
    # Create the root element
    page = etree.Element('results')
    
    # Make a new document tree
    doc = etree.ElementTree(page)
    
    # Add the subelements
    pageElement = etree.SubElement(page, 'Country', 
                                          name='Germany',
                                          Code='DE',
                                          Storage='Basic')
    # For multiple multiple attributes, use as shown above
    
    # Save to XML file
    outFile = open('output.xml', 'w')
    doc.write(outFile, xml_declaration=True, encoding='utf-16') 
    

提交回复
热议问题