[python standard library module five] Xml module learning

Xml module


XML itself is a format specification, a text format specification that contains data and data descriptions. This is what all walks of life used for data exchange before json did not rise. It is also widely used in the financial industry.

For a simple example, xml is a markup language, the format is similar to <data>data</data>, such a closed is a whole

The above is what the inside of the xml looks like, which can be imagined as a tree, as shown in the following figure

Use python to parse xml files

  • xml module import
    import xml.etree.ElementTree as AND

1. Get the relevant information interface of the label

Generally, when accessing an xml file, the root node is obtained first, and then the root node is traversed to obtain information about the child nodes of the root node.

#Get the tree 
root root = tree.getroot()

for element in root:
     print (element.tag) #Get   the tag name 
    print (element.attrib) #Get    the tag attribute 
    print (element.text) #Get the tag value

2. The search operation of the xml file

The lookup operation is done using .iter("tag name"), this function will find the tag matching the tag name and inherit it into an iterable sequence class. This is to search the entire tree from the root node

#Get the text of all "rank" tags 
for element in root.iter( " rank " ):
     print (element.text) #Get the tag value

Find using .findall("tag name"), this function will find all tags matching the tag name integrated into an iterable sequence. This is only available at the current level

#Get all labels with the name country under the root node. You can only find at your own level 
for element in root.findall( " country " ):
     print (element.tag) #Get the tag name

Find using .find("tagname"), this function will find the first tag that matches the tagname. then returns a sequence of subtags of that tag

#Find the tag named country under the root node, and return the sequence of its subtags 
for element in root.find( " country " ):
     print (element.tag) #Get the tag name

3. Deletion of xml

This is divided into modifying the attributes of the tag and the content of the tag

import xml.etree.ElementTree as AND

#Get the xml tree 
tree = ET.parse( " xml_lesson " )

#Get the tree 
root root = tree.getroot()

#Find the tag named country under the root node, and return the sequence consisting of its 
subtags node = root.find( " country " )
node.set("name","China")

node = node.find("rank")
node.text = "1"

#Write back to the original file 
tree.write( " xml_lesson " )

 

 4. xml file creation

The main idea is to create a node first, and then pack the node into a tree

import xml.etree.ElementTree as AND

#Create root node 
root = ET.Element( " data " )
 #Create child nodes and add attributes 
age = ET.SubElement(root, " age " )
age.attrib = {"age":"age attribute"}
age.text = "23"

#Create elementtree object, write file 
tree = ET.ElementTree(root)
tree.write("test.xml")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324798992&siteId=291194637