python 对 XML 的解析 -ElementTree
代碼
__author__ = '未昔/AngelFate'
__date__ = '2019/8/22 20:26'
from xml.etree import ElementTree
class Student_xml:
def __init__(self,studentname=None,age=None,class_=None):
self.studentname=studentname
self.age=age
self.class_=class_
def __str__(self):
print('----转为字符串---')
con = self.studentname+","+self.age+","+self.class_
return con
root_ = ElementTree.parse("E:\python\Study\小经验\data\student.xml")
st = root_.findall("student")
student_list=[]
for conn in st:
student=Student_xml()
student.studentname=conn.find("studentname").text
student.age=conn.find("age").text
student.class_=conn.find("class").text
student_list.append(student)
for con in student_list:
print(con)
结果
D:\import\python3.7\python.exe E:/python/Study/小经验/已发布/python对XML的解析/python对XML的解析-ElementTree.py
----转为字符串---
张三,18,信管171
----转为字符串---
王洋,19,软件178
----转为字符串---
天天,21,网络177
Process finished with exit code 0