Based on Python XML format file example code details

  • 2021-10-11 19:10:11
  • OfStack

XML file is an extensible markup language, a simple data storage language designed to transmit and store data

Some Methods of XML in Python

Read files and contents


# Quote xml Module 
from xml.etree import ElementTree as ET

# ET Go and open it xml Documents 
tree = ET.parse("files/xo.xml")

#  Get root label 
root = tree.getroot()

print(root) # <Element 'data' at 0x7f94e02763b0>

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content) #  Get root label  
print(root) # <Element 'data' at 0x7fdaa019cea0>

Read node data


from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein" id="999" >
    <rank>2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank>69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

#  Get root label  data
root = ET.XML(content)

country_object = root.find("country") #  Get XML In the file country Label 
print(country_object.tag, country_object.attrib)#  Get country Label name    Get country Attribute of label 
gdppc_object = country_object.find("gdppc")#  Get gdppc Label 
print(gdppc_object.tag,gdppc_object.attrib,gdppc_object.text)#  Get gdppc Name of the label    Get gdppc Attribute ( There is no property for :{})  Get gdppc The contents of the label 

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank>2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank>69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

#  Get root label  data
root = ET.XML(content)

#  Get data Children's tags of tags 
for child in root:
  # child.tag = conntry  Get two country Label 
  # child.attrib = {"name":"Liechtenstein"}
  print(child.tag, child.attrib)
  for node in child:
    print(node.tag, node.attrib, node.text) #  Get to reank Label 

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank>2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank>69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content)

#  Find children and grandchildren year Label 
for child in root.iter('year'):
  print(child.tag, child.text)

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank>2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank>69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content)
v1 = root.findall('country') #  Find all the country Label 
print(v1)

v2 = root.find('country').find('rank') #  Find country In the tag rank Label 
print(v2.text)

Delete and modify nodes


from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank>2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank>69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content)

#  Modify node content and properties 
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999" #  Modify rank The contents of the label 
rank.set('update', '2020-11-11') #  For rank Tag addition 1 A update Attribute 
print(rank.text, rank.attrib)
############  Save a file  ############
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')

#  Delete Node 
root.remove( root.find('country') )
print(root.findall('country'))

############  Save a file  ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

Build a document


<home>
  <son name=" Children 1">
    <grandson name=" Children 11"></grandson>
    <grandson name=" Children 12"></grandson>
  </son>
  <son name=" Children 2"></son>
</home>

from xml.etree import ElementTree as ET

#  Create root label 
root = ET.Element("home")

#  Create Node Elder Son 
son1 = ET.Element('son', {'name': ' Children 1'})
#  Create a youngest son 
son2 = ET.Element('son', {"name": ' Children 2'})

#  Create two grandchildren among the eldest son 
grandson1 = ET.Element('grandson', {'name': ' Children 11'})
grandson2 = ET.Element('grandson', {'name': ' Children 12'})
son1.append(grandson1)
son1.append(grandson2)

#  Add a son to the root node 
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False) #short_empty_elements  Is it created as a short label 

<famliy>
  <son name=" Children 1">
    <grandson name=" Children 11"></grandson>
    <grandson name=" Children 12"></grandson>
  </son>
  <son name=" Children 2"></son>
</famliy>

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content) #  Get root label  
print(root) # <Element 'data' at 0x7fdaa019cea0>
0

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content) #  Get root label  
print(root) # <Element 'data' at 0x7fdaa019cea0>
1

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content) #  Get root label  
print(root) # <Element 'data' at 0x7fdaa019cea0>
2

<user><![CDATA[ Hello ]]</user>

from xml.etree import ElementTree as ET

content = """
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2023</year>
    <gdppc>141100</gdppc>
    <neighbor direction="E" name="Austria" />
    <neighbor direction="W" name="Switzerland" />
  </country>
   <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2026</year>
    <gdppc>13600</gdppc>
    <neighbor direction="W" name="Costa Rica" />
    <neighbor direction="E" name="Colombia" />
  </country>
</data>
"""

root = ET.XML(content) #  Get root label  
print(root) # <Element 'data' at 0x7fdaa019cea0>
4

Related articles: