[Python] Use ElementTree to parse xml

introduction

The author refers to the blog on the Internet, welcome to visit the source link: python uses ElementTree to parse xml
and official documents

Examples are as follows:

<?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>

1 analysis

1) Call the parse() method, and after returning the parse tree
python3.3, the ElementTree module will automatically search for available C libraries to speed up

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")  # <class 'xml.etree.ElementTree.ElementTree'>
root = tree.getroot()           # 获取根节点 <Element 'data' at 0x02BF6A80>

2) Call from_string() to return the root element of the parse tree

import xml.etree.ElementTree as ET
data = open("country.xml").read()
root = ET.fromstring(data)   # <Element 'data' at 0x036168A0>

3) Call the ElementTree class ElementTree(self, element=None, file=None) # The element here is the root node

import xml.etree.ElementTree as ET
tree = ET.ElementTree(file="country.xml")  # <xml.etree.ElementTree.ElementTree object at 0x03031390>
root = tree.getroot()  # <Element 'data' at 0x030EA600>

2 traversal

1) Simple traversal

import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")
root = tree.getroot()
print(root.tag, ":", root.attrib)  # 打印根元素的tag和属性

# 遍历xml文档的第二层
for child in root:
    # 第二层节点的标签名称和属性
    print(child.tag,":", child.attrib) 
    # 遍历xml文档的第三层
    for children in child:
        # 第三层节点的标签名称和属性
        print(children.tag, ":", children.attrib)

Nodes can be directly accessed by subscripting

# 访问根节点下第一个country的第二个节点year,获取对应的文本
year = root[0][1].text    # 2008

2) The method provided by ElementTree

 1. find(match)    #查找第一个匹配的子元素, match可以时tag或是xpaht路径
 2. findall(match)   #返回所有匹配的子元素列表
 3. findtext(match, default=None)                     
 4. iter(tag=None)     #以当前元素为根节点 创建树迭代器,如果tag不为None,则以tag进行过滤
 5. iterfind(match)

example:

# 过滤出所有neighbor标签
for neighbor in root.iter("neighbor"):
    print(neighbor.tag, ":", neighbor.attrib)
# 遍历所有的counry标签
for country in root.findall("country"):
    # 查找country标签下的第一个rank标签
    rank = country.find("rank").text
    # 获取country标签的name属性
    name = country.get("name")
    print(name, rank)

3 modify the xml structure

  1. attribute related
# 将所有的rank值加1,并添加属性updated为yes
for rank in root.iter("rank"):
    new_rank = int(rank.text) + 1
    rank.text = str(new_rank)  # 必须将int转为str
    rank.set("updated", "yes") # 添加属性

# 再终端显示整个xml
ET.dump(root)
# 注意 修改的内容存在内存中 尚未保存到文件中
# 保存修改后的内容
tree.write("output.xml")
import xml.etree.ElementTree as ET

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

for rank in root.iter("rank"):
    # attrib为属性字典
    # 删除对应的属性updated
    del rank.attrib['updated']  

ET.dump(root)

Summary: About class xml.etree.ElementTree.Element attribute related

1.attrib     # 为包含元素属性的字典
2.keys()     # 返回元素属性名称列表
3.items()    # 返回(name,value)列表
4.get(key, default=None)    # 获取属性
5.set(key, value)        # 跟新/添加  属性
6.del xxx.attrib[key]       # 删除对应的属性
  1. Node/element related
    delete child element remove()
import xml.etree.ElementTree as ET

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

# 删除rank大于50的国家
for country in root.iter("country"):
    rank = int(country.find("rank").text)
    if rank > 50:
        # remove()方法 删除子元素
        root.remove(country)
ET.dump(root)
tree.write('country.xml')  # 保存修改

Add child element
code:

import xml.etree.ElementTree as ET

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

country = root[0]
last_ele = country[len(list(country))-1]
last_ele.tail = '\n\t\t'
# 创建新的元素, tag为test_append
elem1 = ET.Element("test_append")
elem1.text = "elem 1"
# elem.tail = '\n\t'
country.append(elem1)

# SubElement() 其实内部调用的时append()
elem2 = ET.SubElement(country, "test_subelement")
elem2.text = "elem 2"

# extend()
elem3 = ET.Element("test_extend")
elem3.text = "elem 3"
elem4 = ET.Element("test_extend")
elem4.text = "elem 4"
country.extend([elem3, elem4])

# insert()
elem5 = ET.Element("test_insert")
elem5.text = "elem 5"
country.insert(5, elem5)

ET.dump(country)
tree.write('country.xml')  # 保存修改

Summary of methods for adding child elements:

 1. append(subelement)
 2. extend(subelements)
 3. insert(index, element)

4. Create xml document

I want to create a root Element, then create a SubElement, and finally pass the root element into ElementTree(element), create a tree, and call the tree.write() method to write to the file

For the three methods of creating elements: use ET.Element, the makeelement() method of the Element object and ET.SubElement

import xml.etree.ElementTree as ET


def subElement(root, tag, text):
    ele = ET.SubElement(root, tag)
    ele.text = text
    ele.tail = '\n'


root = ET.Element("note")

to = root.makeelement("to", {
    
    })
to.text = "peter"
to.tail = '\n'
root.append(to)

subElement(root, "from", "marry")
subElement(root, "heading", "Reminder")
subElement(root, "body", "Don't forget the meeting!")

tree = ET.ElementTree(root)
tree.write("note.xml", encoding="utf-8", xml_declaration=True)

Since the natively saved XML has no indentation by default, if you want to set the indentation, you need to modify the save method
code:

import xml.etree.ElementTree as ET
from xml.dom import minidom


def subElement(root, tag, text):
    ele = ET.SubElement(root, tag)
    ele.text = text


def saveXML(root, filename, indent="\t", newl="\n", encoding="utf-8"):
    rawText = ET.tostring(root)
    dom = minidom.parseString(rawText)
    with open(filename, 'w') as f:
        dom.writexml(f, "", indent, newl, encoding)


root = ET.Element("note")

to = root.makeelement("to", {
    
    })
to.text = "peter"
root.append(to)

subElement(root, "from", "marry")
subElement(root, "heading", "Reminder")
subElement(root, "body", "Don't forget the meeting!")

# 保存xml文件
saveXML(root, "note.xml")

Guess you like

Origin blog.csdn.net/qq_44703886/article/details/120972333