[Python 2.x] xml.etree.ElementTree module

Source: Internet
Author: User

XML File: Xmlparse.xml

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><!DOCTYPE Model SYSTEM "/var/company/user/etc/user2017.dtd"><Modelversion= "1"importversion= "16.2">    <Create>        < Companyname= "Google.inc">            <Divisionsourcetype= "Personnel">                <UserNamestring= "John Smith" />                <Sexstring= "Male" />                < Ageint= "+" />                <HireDatestring= "2009-07-01" />                < StationSting= "Manager" />                <IsmarryBoolen= "True" />            </Division>            <Divisionsourcetype= "Personnel">                <UserNamestring= "Mary Smith" />                <Sexstring= "Female" />                < Ageint= "All" />                <HireDatestring= "2017-07-01" />                < Stationstring= "Secretary" />                <IsmarryBoolen= "False" />            </Division>        </ Company>        < Companyname= "Baidu.inc">            <Divisionsourcetype= "Personnel">                <UserNamestring= "Alice Wang" />                <Sexstring= "Female" />                < Ageint= "$" />                <HireDatestring= "2002-07-01" />                < Stationstring= "HR Manager" />                <IsmarryBoolen= "True" />            </Division>            <Divisionsourcetype= "Personnel">                <UserNamestring= "Mark Zhou" /><sex string= "Male"/>
< Ageint= " the" /> <HireDatestring="" /> < Stationstring= "Intern" /> <IsmarryBoolen= "False" /> </Division> </ Company> </Create></Model>

Parse the XML file and print the age of each individual company: testparse.py

ImportXml.etree.ElementTree as Ettree= Et.parse ('Xmlparse.xml') Root=tree.getroot () forDivisioninchRoot.findall ('.//division'): UserName= Division.find ('UserName'). Attrib age= Division.find (' Age'). AttribPrintUserName, age

Output Result:

{'string':'John Smith'} {'int':' -'}{'string':'Mary Smith'} {'int':' at'}{'string':'Alice Wang'} {'int':' in'}{'string':'Mark Zhou'} {'int':' -'}

[Python 2.x] xml.etree.ElementTree module

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.