Home python How to create xml on python using xml.etree.elementTree?

How to create xml on python using xml.etree.elementTree?

Author

Date

Category

Create an XML of the following code:

import xml.etree.elementtree as et
 ... Some Code to Create Xml ...
MyData = et.Tostring (Root, Encoding = "UTF-8", Method = "XML")
MyFile = Open ("test_1.xml", "WB")
MyFile.Write (MyData)

and get XML in the row. I understand this because of the toostring function, but I can not find / implement what it would be as an example below

& lt; xml version = "1.0"? & gt;
& lt; Data & gt;
  & lt; Country Name = "Liechtenstein" & gt;
    & lt; Rank Updated = "Yes" & gt; 2 & lt; / rank & gt;
    & lt; Year & gt; 2008 & lt; / year & gt;
    & lt; GDPPC & GT; 141100 & lt; / GDPPC & GT;
    & lt; neighbor name = "austria" direction = "e" / & gt;
    & lt; neighbor name = "Switzerland" direction = "w" / & gt;
  & LT; / Country & gt;
  & lt; Country Name = "Singapore" & gt;
    & lt; Rank Updated = "Yes" & gt; 5 & lt; / Rank & GT;
    & lt; year & gt; 2011 & lt; / year & gt;
    & lt; GDPPC & GT; 59900 & lt; / GDPPC & GT;
    & lt; neighbor name = "Malaysia" direction = "n" / & gt;
  & LT; / Country & gt;
& lt; / data & gt;

Answer 1, Authority 100%

To create XML Use the et.element objects :

  • designer et.element will allow you to specify the title of the tag, and its named parameters in the attributes.
  • contents set via .text
  • And adding nested tags via .append
  • method

An example :

import xml.etree.elementtree as et
root = et.element ('Data')
Country = et.element ('Country', name = "Liechtenstein")
Rank = et.Element ('Rank', updated = "Yes")
rank.text = '2'
Country.append (Rank)
Year = et.element ('YEAR')
year.text = '2008'
Country.APPEND (YEAR)
GDPPC = et.Element ('GDPPC')
gdppc.text = '141100'
Country.append (GDPPC)
Country.append (Et.Element ('Neighbor', name = "Austria", Direction = "E"))
Country.append (Et.Element ('Neighbor', name = "Switzerland", Direction = "W"))
root.append (country)
Country = et.Element ('Country', name = "Singapore")
root.append (country)
# NOTE: By analogy above, fill in the countries you need
xml_str = et.tostring (root, encoding = "UTF-8", Method = "XML")
print (xml_str.decode (encoding = "UTF-8"))
# & lt; Data & gt;
# & lt; Country name = "Liechtenstein" & gt;
# & lt; Rank Updated = "Yes" & gt; 2 & lt; / rank & gt;
# & lt; Year & gt; 2008 & lt; / year & gt;
# & lt; GDPPC & GT; 141100 & lt; / GDPPC & GT;
# & lt; neighbor direction = "e" name = "austria" / & gt;
# & lt; neighbor direction = "w" name = "switzerland" / & gt;
# & LT; / Country & gt;
# & lt; Country name = "Singapore" / & gt;
# & LT; / DATA & GT;

built in xml.etree.elementtree Features to get XML not found, but This is possible :

def indent (elem, level = 0):
  i = "\ n" + level * ""
  IF LEN (ELEM):
    IF Not Elem.Text.strip ():
      elem.text = i + ""
    If not elem.tail or not elem.tail.strip ():
      Elem.Tail = I.
    For Elem in Elem:
      INDENT (ELEM, LEVEL + 1)
    If not elem.tail or not elem.tail.strip ():
      Elem.Tail = I.
  ELSE:
    If Level and (Not Elem.Tail or Not elem.tail.strip ()):
      Elem.Tail = I.

Before saving XML in the string or file, call INDENT (ROOT) :

...
INDENT (ROOT)
xml_str = et.tostring (root, encoding = "UTF-8", Method = "XML")

And to get XML declaration , the line of the type & lt;? xml version = '1.0' encoding = 'UTF-8'? & GT; , you need to Et.ElementTree Save XML :

etree = et.elementtree (root)
F = io.Bytesio ()
Etree.Write (F, Encoding = 'UTF-8', XML_DECLARATION = TRUE)
Print (F.GetValue (). Decode (Encoding = "UTF-8"))
# & lt;? xml version = '1.0' encoding = 'utf-8'? & gt;
# & lt; Data & gt;
# & lt; Country name = "Liechtenstein" & gt;
# & lt; Rank Updated = "Yes" & gt; 2 & lt; / rank & gt;
...
# To write immediately to the file:
MyFile = Open ("test_1.xml", "WB")
Etree.Write (MyFile, Encoding = 'UTF-8', XML_DECLARATION = TRUE)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions