Python parsing xml
ImportXml.dom.minidom as Minidomdom= Minidom.parse ("Aa.xml") Root= Dom.getelementsbytagname ("Schools")#The function getElementsByTagName returns NodeList.Print(root.length) forNodeinchRoot:Print("Root element is%s. "%node.tagname)#formatted output differs greatly from the C-series language. Schools = Node.getelementsbytagname ("School") forSchoolinchSchools:Print("School.nodename", School.nodename)Print("School.tagname:", School.tagname)Print("School.getattribute:", School.getattribute ("Name")) Print("School.getAttribute.value:", school.attributes["Name"].value) Classes= School.getelementsbytagname ("Class") Print("There is%d classes in school%s"% (Classes.length, School.getattribute ("Name"))) forMClassinchclasses:Print("Mclass.getattribute", Mclass.getattribute ("Id")) forStudentinchMclass.getelementsbytagname ("Student"): Print(student.attributes["Name"].value)#Print (Student.getelementsbytagname ("中文版") [0].nodevalue] #这个为什么啊? #Print (Student.getelementsbytagname ("中文版") [0].childnodes[0].nodevalue) Print(Student.getelementsbytagname ('ll') [0].childnodes[0].nodevalue] Student.getelementsbytagname ("ll") [0].childnodes[0].nodevalue = 75#getElementsByTagName Returns a collection #ChildNodes returned is also a collectionF= Open ('New.xml','W', encoding ='Utf-8') Dom.writexml (f,encoding='Utf-8') F.close ()
<?XML version= "1.0" encoding= "Utf-8"?><Schools> <SchoolName= "Xidian"> <ClassId= "030612"> <StudentName= "Salomon"> <Scores> <Math>98</Math> <中文版> <ll>85</ll> </中文版> <Physics>89</Physics> </Scores> </Student> <StudentName= "Jupiter"> <Scores> <Math>74</Math> <中文版> <ll>83</ll> </中文版> <Physics>69</Physics> </Scores> </Student> </Class> <ClassId= "030611"> <StudentName= "Venus"> <Scores> <Math>98</Math> <中文版> <ll>83</ll> </中文版> <Physics>89</Physics> </Scores> </Student> <StudentName= "Mars"> <Scores> <Math>74</Math> <中文版> <ll>83</ll> </中文版> <Physics>69</Physics> </Scores> </Student> </Class> </School></Schools>
Python parsing xml