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文件:
#加载文件
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>
相关文章推荐
- python模块之HTMLParser: 解析html,获取url
- Python技巧_处理带参数的超长URL
- random GB2312 in python
- python学习聚合
- ubuntu python up
- python中的反射
- Python正则表达式--前向界定符
- 实例教程:1小时学会Python(转)
- Python之random
- ubuntu下安装eclipse以及配置python编译环境
- 002_008 Python 随机写入文件,更新随机存取的文件
- Combining Hive and Python
- python print 追加写入文件中
- 用python flask搭建一个网站[三]——表单
- python学习笔记-10(类class)
- Python通过递归遍历出集合中所有元素的方法
- python _、__和__xx__的区别
- 【Python学习笔记】Flaky插件自动运行失败的测试
- Python下opencv使用笔记(二)(简单几何图像绘制)