Simple Use of python xml Module

  • 2021-09-20 21:01:53
  • OfStack

1. Introduction to xml

xml is a protocol for data exchange between different languages or programs, which is similar to json, but json is simpler to use. However, in ancient times, in the dark years before json was born, everyone could only choose xml. Up to now, the interface of many traditional companies, such as many systems in the financial industry, is mainly xml.

The format of xml is as follows, that is, through < > Node to distinguish data structures:


<?xml version="1.0"?>
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
  </country>
  <country name="Singapore">
    <rank updated="yes">5</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
  </country>
  <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2011</year>
    <gdppc>13600</gdppc>
    <neighbor name="Costa Rica" direction="W"/>
    <neighbor name="Colombia" direction="E"/>
  </country>
</data>

2. Python uses xml

The xml protocol is supported in all languages. In python, the following modules can be used to operate xml:


# print(root.iter('year')) # Full-text search 
# print(root.find('country')) # In root Find the child nodes of, only find 1 A 
# print(root.findall('country')) # In root Find the child nodes of, find all 

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

# Traversal xml Document 
for child in root:
  print('========>', child.tag, child.attrib, child.attrib['name'])
  for i in child:
    print(i.tag, i.attrib, i.text)

# Traversing only year  Node 
for node in root.iter('year'):
  print(node.tag, node.text)
#---------------------------------------

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()

# Modify 
for node in root.iter('year'):
  new_year = int(node.text) + 1
  node.text = str(new_year)
  node.set('updated', 'yes')
  node.set('version', '1.0')
tree.write('test.xml')

# Delete node
for country in root.findall('country'):
  rank = int(country.find('rank').text)
  if rank > 50:
    root.remove(country)

tree.write('output.xml')

# In country Add ( append ) Node year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root = tree.getroot()
for country in root.findall('country'):
  for year in country.findall('year'):
    if int(year.text) > 2000:
      year2 = ET.Element('year2')
      year2.text = ' New Year '
      year2.attrib = {'update': 'yes'}
      country.append(year2) # To country Add child nodes under node 

tree.write('a.xml.swap')

3. Create your own xml document


import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
sex = ET.SubElement(name, "sex")
sex.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'

et = ET.ElementTree(new_xml) # Generate a document object 
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml) # Print the generated format 

The above is the python xml module simple use of the details, more information about the use of python xml module please pay attention to other related articles on this site!


Related articles: