Read XML File Python: Simple Tutorial
Python has the ability to read XML files using the ElementTree module. Here is a simple example code:
import xml.etree.ElementTree as ET
# 加载XML文件
tree = ET.parse('example.xml')
root = tree.getroot()
# 遍历XML文件的节点
for elem in root.iter():
# 打印节点的标签和文本内容
print(elem.tag, elem.text)
The code above assumes the existence of an XML file named example.xml, then loads the XML file using the ET.parse() function and retrieves the root node using tree.getroot(). Next, it traverses all nodes in the XML file using root.iter(), and can use elem.tag to get the tag of the node and elem.text to get the text content of the node. In this example, it simply prints out the tag and text content of the nodes.
This is just a basic use case for reading XML files, in actual applications, it may be necessary to handle and parse the XML structure according to specific needs.