Python parsing xml

Source: Internet
Author: User
Tags tagname

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.