python3 xml.dom.mini模块解析xml

本文介绍使用Python的xml.dom.minidom模块解析XML文件的方法。通过示例代码展示如何获取XML中特定标签的数据,并处理不同类型的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

impoort  xml.dom.minidom
#加载文件
dom = xml.dom.minidom.parse("weather.xml")
dom = xml.dom.minidom.parse(open("weather.xml"))
#加载字符串
dom = xml.dom.minidom.parseString(xmlString)
type(dom)--->Document类型
root = dom.documentElement返回dom的根元素
root的子节点是Node类型的子类
根据node对象的nodeType属性判断具体是哪个子类:
nodeType:取值
‘ATTRIBUTE_NODE’
‘CDATA_SECTION_NODE’
‘COMMENT_NODE’
‘DOCUMENT_FRAGMENT_NODE’
‘DOCUMENT_NODE’
‘DOCUMENT_TYPE_NODE’
‘ELEMENT_NODE’
‘ENTITY_NODE’
‘ENTITY_REFERENCE_NODE’
‘NOTATION_NODE’
‘PROCESSING_INSTRUCTION_NODE’
‘TEXT_NODE’
如何获得当前节点的子节点: 注意 xml.dom.minidom方式的时候一定要进行类型判断
a)知道子节点名 --->返回此节点下所有节点名满足条件的子节点
nodelist = root.getElementsByTagName(TagName)根据标签名获得节点,type NodeList
b)不知道子节点名  --->返回此节点的第一层子节点
nodelist = root.childNodes -->类型也NodeList
最后对nodeList遍历判断node的类型进行处理
示例:
def miniDomParseXML(text=""):
if "" == text:
raise RuntimeError("解析的内容不能为空")
docu = xml.dom.minidom.parseString(text)
root = docu.documentElement
getNodeText(root)


def getNodeText(ele):
if ele.nodeType == xml.dom.minidom.Node.ELEMENT_NODE:
for e in ele.childNodes:
getNodeText(e)
elif ele.nodeType in (xml.dom.minidom.Node.TEXT_NODE,xml.dom.minidom.Node.CDATA_SECTION_NODE):
print(ele.nodeValue.strip())
*****************************************************************
示例xml文件:
<CityWeatherResponse>
    <status>success</status>
    <date>2014-04-27</date>
    <results>
        <currentCity>双牌</currentCity>
        <weather_data>
            <date>周六(实时:16℃)</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/yin.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/yin.png
            </nightPictureUrl>
            <weather>阴</weather>
            <wind>北风微风</wind>
            <temperature>15℃</temperature>
            <date>周日</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/duoyun.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/duoyun.png
            </nightPictureUrl>
            <weather>多云</weather>
            <wind>北风微风</wind>
            <temperature>20 ~ 15℃</temperature>
            <date>周一</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/duoyun.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/duoyun.png
            </nightPictureUrl>
            <weather>多云</weather>
            <wind>北风微风</wind>
            <temperature>22 ~ 15℃</temperature>
            <date>周二</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/zhenyu.png
            </dayPictureUrl>
            <nightPictureUrl>
                 http://api.map.baidu.com/images/weather/night/zhenyu.png
            </nightPictureUrl>
            <weather>阵雨</weather>
            <wind>北风微风</wind>
            <temperature>20 ~ 15℃</temperature>
        </weather_data>
        <currentCity>长沙</currentCity>
        <weather_data>
            <date>周六(实时:16℃)</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/zhenyu.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/zhenyu.png
            </nightPictureUrl>
            <weather>阵雨</weather>
            <wind>微风</wind>
            <temperature>15℃</temperature>
            <date>周日</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/duoyun.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/duoyun.png
            </nightPictureUrl>
            <weather>多云</weather>
            <wind>微风</wind>
            <temperature>21 ~ 15℃</temperature>
            <date>周一</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/duoyun.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/duoyun.png
            </nightPictureUrl>
            <weather>多云</weather>
            <wind>微风</wind>
            <temperature>24 ~ 15℃</temperature>
            <date>周二</date>
            <dayPictureUrl>
                http://api.map.baidu.com/images/weather/day/duoyun.png
            </dayPictureUrl>
            <nightPictureUrl>
                http://api.map.baidu.com/images/weather/night/duoyun.png
            </nightPictureUrl>
            <weather>多云</weather>
            <wind>北风微风</wind>
            <temperature>24 ~ 14℃</temperature>
        </weather_data>
    </results>
</CityWeatherResponse>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值