![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="utf-8" ?> <request> <functionID>subPackageInfo</functionID> <time>2014-02-10 15:10:50</time> <packageList> <packageInfo> <orderId>22088315090</orderId> <expressName>EMS</expressName> <expressTel>01</expressTel> <expressNo>0001</expressNo> <productId>1001173023</productId> <allotQuatity>5</allotQuatity> <outOfStockQuatity>0</outOfStockQuatity> <promotionID></promotionID> </packageInfo> <packageInfo> <orderId>22088315090</orderId> <expressName>EMS</expressName> <expressTel>010-55675233</expressTel> <expressNo>0001</expressNo> <productId>1001173123</productId> <allotQuatity>5</allotQuatity> <outOfStockQuatity>0</outOfStockQuatity> <promotionID>-1</promotionID> </packageInfo> <packageInfo> <orderId>22088315090</orderId> <expressName>EMS</expressName> <expressTel>010-55675233</expressTel> <expressNo>0001</expressNo> <productId>1001173223</productId> <allotQuatity>0</allotQuatity> <outOfStockQuatity>1</outOfStockQuatity> <promotionID>-1</promotionID> </packageInfo> </packageList> </request>
首先把以上xml文件保存到本地
导入xml模块,使用dom.minidom导入xml文件
#coding:utf-8
from xml.dom import minidom
dom=minidom.parse('22088315090.xml')
>>> packs=dom.getElementsByTagName('packageInfo')
查找xml下面的所有packageInfo节点
>>> pack_childnotes=packs[0].childNodes
查找第一个packageInfo的所有子节点
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
>>> for i in pack_childnotes:print '###',i.toxml() ### ### <orderId>22088315090</orderId> ### ### <expressName>EMS</expressName> ### ### <expressTel>01</expressTel> ### ### <expressNo>0001</expressNo> ### ### <productId>1001173023</productId> ### ### <allotQuatity>5</allotQuatity> ### ### <outOfStockQuatity>0</outOfStockQuatity> ### ### <promotionID/> ###
以上打印出所有类型的xml信息,包括元素类型和文本类型
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
>>> for i in pack_childnotes: if i.nodeType==i.TEXT_NODE: pass else: print 'name',i.nodeName tagname=i.nodeName if i.childNodes: print 'type',i.childNodes[0].data value=i.childNodes[0].data else: value='' orders_info[tagname]=value print orders_info {u'orderId': u'22088315090', u'promotionID': '', u'outOfStockQuatity': u'0', u'expressNo': u'0001', u'expressTel': u'01', u'productId': u'1001173023', u'expressName': u'EMS', u'allotQuatity': u'5'}
把第一个packageInfo的子节点数据拿到保存在字典中
完整的代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#coding:utf-8 from xml.dom import minidom dom=minidom.parse('22088315090.xml') packs=dom.getElementsByTagName('packageInfo') orders=[] #pack_childnotes=packs[0].childNodes def control(pack): pack_childnotes=pack.childNodes orders_info={} for i in pack_childnotes: if i.nodeType==i.TEXT_NODE: pass else: #print 'name',i.nodeName tagname=i.nodeName if i.childNodes: #print 'type',i.childNodes[0].data value=i.childNodes[0].data else: value='' orders_info[tagname]=value return orders_info for pack in packs: #print pack orders.append(control(pack)) print orders