Python learning: Getting tag Properties

Source: Internet
Author: User

File Info.xml

<?xml version= "1.0" encoding= "Utf-8"?><info> <base> <platform>Windows</platform> < browser>firefox</browser> <url>http://www.baidu.com</url> <login username= "admin" password = "123456"/> <login username= "Guest" password= "654321"/> </base> <test> <province> Beijing </ province> <province> Guangdong </province> <city> shenzhen </city> <city> Zhuhai </city>< province> Zhejiang </province> <city> Hangzhou </city> </test></info>
    1. File read_xml_1.py: Get any label signature

#coding =utf-8import xml.dom.minidom Dom =xml.dom.minidom.parse (' e:\\selenium_relatived\\learning\\info.xml ') root = Dom.documentelementprint (root.nodename) tagname = root.getelementsbytagname (' browser ') print (tagname[0].tagname) tagname1 = root.getelementsbytagname (' login ') print (tagname1[1].tagname) tagname2 = Root.getelementsbytagname (' Province ') print (tagname2[2].tagname)

getElementsByTagName gets the label by its tag name, and the object it obtains is stored as an array


2. File read_xml_2.py: Get the properties of a tag

#coding =utf-8import xml.dom.minidom Dom =xml.dom.minidom.parse (' e:\\selenium_relatived\\learning\\info.xml ') root = Dom.documentelementprint (root.nodename) logins = root.getelementsbytagname (' login ') username=logins[0]. GetAttribute (' username ') print (username) username1=logins[1].getattribute (' username ') print (username1)

3. Get the data between label pairs

#coding =utf-8import xml.dom.minidom Dom =xml.dom.minidom.parse (' e:\\selenium_relatived\\learning\\info.xml ') root = Dom.documentelementprint (root.nodename) province = Dom.getelementsbytagname (' province ') Citys = Dom.getelementsbytagname (' City ') P2=province[1].firstchild.dataprint (p2) c1 = Citys[0].firstchild.dataprint (C1)


Python learning: Getting tag Properties

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.