文件info.xml<?xml version="1.0" encoding="utf-8"?>
Windows
Firefox
http://www.baidu.com
北京
广东
深圳
珠海
浙江
杭州
文件read_xml_1.py:获取任意标签名#coding=utf-8
import xml.dom.minidom
dom =xml.dom.minidom.parse(‘E:\\Selenium_Relatived\\learning\\info.xml‘)
root =dom.documentElement
print(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通过标签名获取标签,它所获得的对象是以数组形式存放
2.文件read_xml_2.py:获取标签的属性#coding=utf-8
import xml.dom.minidom
dom =xml.dom.minidom.parse(‘E:\\Selenium_Relatived\\learning\\info.xml‘)
root =dom.documentElement
print(root.nodeName)
logins = root.getElementsByTagName(‘login‘)
username=logins[0].getAttribute(‘username‘)
print(username)
username1=logins[1].getAttribute(‘username‘)
print(username1)
3.获取标签对之间的数据#coding=utf-8
import xml.dom.minidom
dom =xml.dom.minidom.parse(‘E:\\Selenium_Relatived\\learning\\info.xml‘)
root =dom.documentElement
print(root.nodeName)
province = dom.getElementsByTagName(‘province‘)
citys = dom.getElementsByTagName(‘city‘)
p2=province[1].firstChild.data
print(p2)
c1 = citys[0].firstChild.data
print(c1)