您的位置:首页 > 编程语言 > Python开发

python3 xml.dom.mini模块解析xml

2014-04-27 15:27 337 查看
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>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: