<?xml version="1.0" encoding="utf-8"?>
<Schools>
<School Name="XiDian">
<Class Id="030612">
<Student Name="salomon">
<Scores>
<Math>98</Math>
<English>85</English>
<physics>89</physics>
</Scores>
</Student>
<Student Name="Jupiter">
<Scores>
<Math>74</Math>
<English>83</English>
<physics>69</physics>
</Scores>
</Student>
</Class>
<Class Id="030611">
<Student Name="Venus">
<Scores>
<Math>98</Math>
<English>85</English>
<physics>89</physics>
</Scores>
</Student>
<Student Name="Mars">
<Scores>
<Math>74</Math>
<English>83</English>
<physics>69</physics>
</Scores>
</Student>
</Class>
</School>
</Schools>
'''
Created on 2012-5-25
@author: salomon
'''
import xml.dom.minidom as minidom
dom = minidom.parse("E:\\test.xml")
root = dom.getElementsByTagName("Schools") #The function getElementsByTagName returns NodeList.
print(root.length)
for node in root:
print("Root element is %s。" %node.tagName)# 格式化输出,与C系列语言有很大区别。
schools = node.getElementsByTagName("School")
for school in schools:
print(school.nodeName)
print(school.tagName)
print(school.getAttribute("Name"))
print(school.attributes["Name"].value)
classes = school.getElementsByTagName("Class")
print("There are %d classes in school %s" %(classes.length, school.getAttribute("Name")))
for mclass in classes:
print(mclass.getAttribute("Id"))
for student in mclass.getElementsByTagName("Student"):
print(student.attributes["Name"].value)
print(student.getElementsByTagName("English")[0].nodeValue) #这个为什么啊?
print(student.getElementsByTagName("English")[0].childNodes[0].nodeValue)
student.getElementsByTagName("English")[0].childNodes[0].nodeValue = 75
f = open('new.xml', 'w', encoding = 'utf-8')
dom.writexml(f,encoding = 'utf-8')
f.close()
'''
Created on 2012-5-25
@author: salomon
'''
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("E:\\test.xml")
root = tree.getroot()
print(root.tag)
print(root[0].tag)
print(root[0].attrib)
schools = root.getchildren()
for school in schools:
print(school.get("Name"))
classes = school.findall("Class")
for mclass in classes:
print(mclass.items())
print(mclass.keys())
print(mclass.attrib["Id"])
math = mclass.find("Student").find("Scores").find("Math")
print(math.text)
math.set("teacher", "bada")
tree.write("new.xml")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有