Example of creating an xml file in Python and example of pythonxml

Source: Internet
Author: User

Example of creating an xml file in Python and example of pythonxml

This example describes how to create an xml file in Python. We will share this with you for your reference. The details are as follows:

This is an example of generating an xml file using the ElementTree class library.

# *-* coding=utf-8from xml.etree.ElementTree import ElementTreefrom xml.etree.ElementTree import Elementfrom xml.etree.ElementTree import SubElementfrom xml.etree.ElementTree import dumpfrom xml.etree.ElementTree import Commentfrom xml.etree.ElementTree import tostringimport osfilename="book.xml"def CreateXml():  book =ElementTree()  purOrder =Element("PurchaseOrder")  book._setroot(purOrder)  list = Element("account",{'idsn':'2390094'})  purOrder.append(list)  item = Element("item1",{"sku":"abcd","qty":"4"})  SubElement(item,"Name").text="Potato Smasher"  SubElement(item,"Description").text="Smash Potatoes like never before"  purOrder.append(item)  item = Element("item2",{"sku":"gfhi","qty":"40"})  SubElement(item,"Name").text="Beijing"  SubElement(item,"Description").text="My Country"  purOrder.append(item)  indent(purOrder)  return bookdef indent(elem,level=0):  i ="\n"+level*"  "  print elem;  if len(elem):    if not elem.text or not elem.text.strip():      elem.text = i + "  "    for e in elem:      print e      indent(e,level+1)    if not e.tail or not e.tail.strip():      e.tail =i  if level and (not elem.tail or not elem.tail.strip()):    elem.tail =i  return elemif __name__ == '__main__':  book =CreateXml()  book.write(filename,"utf-8")  #book.write("book2.xml","utf-8",True) #true is with xml declaration

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.