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>
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