zoukankan      html  css  js  c++  java
  • python解析xml

    python解析xml

    import xml.dom.minidom as minidom
    
    
    dom = minidom.parse("aa.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",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 are %d classes in school %s" %(classes.length, school.getAttribute("Name")))
            for mclass in classes:
                print("mclass.getAttribute",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)
                    print(student.getElementsByTagName('ll')[0].childNodes[0].nodeValue)
                    student.getElementsByTagName("ll")[0].childNodes[0].nodeValue = 75
                    #getElementsByTagName返回的是一个集合
                    #childNodes返回的也是一个集合
    
    f =  open('new.xml',  'w', encoding = 'utf-8')
    dom.writexml(f,encoding = 'utf-8')
    f.close()
    <?xml version="1.0" encoding="utf-8"?>
    <Schools>
        <School Name="XiDian">
            <Class Id="030612">
                <Student Name="salomon">
                    <Scores>
                        <Math>98</Math>
                        <English>
                            <ll>85</ll>
                        </English>
                        <physics>89</physics>
                    </Scores>
                </Student>
                <Student Name="Jupiter">
                    <Scores>
                        <Math>74</Math>
                        <English>
                            <ll>83</ll>
                        </English>
                        <physics>69</physics>
                    </Scores>
                </Student>
            </Class>
            <Class Id="030611">
                <Student Name="Venus">
                    <Scores>
                        <Math>98</Math>
                        <English>
                             <ll>83</ll>
                        </English>
                        <physics>89</physics>
                    </Scores>
                </Student>
                <Student Name="Mars">
                    <Scores>
                        <Math>74</Math>
                        <English>
                             <ll>83</ll>
                        </English>
                        <physics>69</physics>
                    </Scores>
                </Student>
            </Class>
        </School>
    </Schools>
  • 相关阅读:
    pamamiko的学习笔记
    pamamiko的安装
    python基本语法1.1--十进制与十六进制数之间的相互转换
    mnist深入--用卷积神经网络改善mnist
    minst数据集怎么跑
    七大常见排序算法总结(Java语言)
    ubuntu 16.03 Anaconda Tensorflow(CPU)安装
    IntelliJ Idea 2017 免费激活方法
    Ubuntu下安装deb格式的软件
    win10下配置java环境
  • 原文地址:https://www.cnblogs.com/liguangao/p/5176214.html
Copyright © 2011-2022 走看看