python解析xml文件操作实例


Posted in Python onOctober 05, 2014

本文实例讲述了python解析xml文件操作的实现方法。分享给大家供大家参考。具体方法如下:

xml文件内容如下:

<?xml version="1.0" ?> 
<!--Simple xml document__chapter 8--> 
<book> 
  <title> 
    sample xml thing 
  </title> 
  <author> 
    <name> 
      <first> 
        ma 
      </first> 
      <last> 
        xiaoju 
      </last> 
    </name> 
    <affiliation> 
      Springs Widgets, Inc. 
    </affiliation> 
  </author> 
  <chapter number="1"> 
    <title> 
      First 
    </title> 
    <para> 
      I think widgets are greate.You should buy lots of them forom 
      <company> 
        Spirngy Widgts, Inc 
      </company> 
    </para> 
  </chapter> 
</book>

python代码:

from xml.dom import minidom, Node 
import re, textwrap 
 
class SampleScanner: 
  """""" 
 
  def __init__(self, doc): 
    """Constructor""" 
    assert(isinstance(doc, minidom.Document)) 
    for child in doc.childNodes: 
      if child.nodeType == Node.ELEMENT_NODE and \ 
        child.tagName == "book": 
        self.handle_book(child) 
         
  def handle_book(self, node): 
     
    for child in node.childNodes: 
      if child.nodeType != Node.ELEMENT_NODE: 
        continue 
      if child.tagName == "title": 
        print "Book titile is:", self.gettext(child.childNodes) 
      if child.tagName == "author": 
        self.handle_author(child) 
      if child.tagName == "chapter": 
        self.handle_chapter(child) 
         
  def handle_chapter(self, node): 
    number = node.getAttribute("number") 
    print "number:", number 
    title_node = node.getElementsByTagName("title") 
    print "title:", self.gettext(title_node) 
     
    for child in node.childNodes: 
      if child.nodeType != Node.ELEMENT_NODE: 
        continue 
      if child.tagName == "para": 
        self.handle_chapter_para(child) 
         
  def handle_chapter_para(self, node): 
    company = "" 
    company = self.gettext(node.getElementsByTagName("company")) 
    print "chapter:para:company", company 
     
         
  def handle_author(self, node): 
    for child in node.childNodes: 
      if child.nodeType != Node.ELEMENT_NODE: 
        continue 
      if child.tagName == "name": 
        self.handle_author_name(child) 
      if child.tagName == "affiliation": 
        print "affiliation:", self.gettext(child.childNodes) 
         
  def handle_author_name(self, node): 
    first = "" 
    last = "" 
    for child in node.childNodes: 
      if child.nodeType != Node.ELEMENT_NODE: 
        continue 
      if child.tagName == "first": 
        first = self.gettext(child.childNodes) 
      if child.tagName == 'last': 
        last = self.gettext(child.childNodes) 
         
    print "firstname:%s,lastname:%s" % (first, last) 
     
         
  def gettext(self, nodelist): 
    retlist = [] 
    for node in nodelist: 
      if node.nodeType == Node.TEXT_NODE: 
        retlist.append(node.wholeText) 
      elif node.hasChildNodes: 
        retlist.append(self.gettext(node.childNodes)) 
         
    return re.sub('\s+', " ", ''.join(retlist)) 
   
         
if __name__=="__main__": 
  doc = minidom.parse("simple.xml") 
  sample = SampleScanner(doc)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
利用Python实现简单的相似图片搜索的教程
Apr 23 Python
Python中如何获取类属性的列表
Dec 26 Python
Python3读取Excel数据存入MySQL的方法
May 04 Python
关于Django ForeignKey 反向查询中filter和_set的效率对比详解
Dec 15 Python
详解10个可以快速用Python进行数据分析的小技巧
Jun 24 Python
Python为何不能用可变对象作为默认参数的值
Jul 01 Python
python3图片文件批量重命名处理
Oct 31 Python
使用IDLE的Python shell窗口实例详解
Nov 19 Python
Python如何使用turtle库绘制图形
Feb 26 Python
Python识别验证码的实现示例
Sep 30 Python
python中xlrd模块的使用详解
Feb 01 Python
利用Selenium添加cookie实现自动登录的示例代码(fofa)
May 08 Python
python写xml文件的操作实例
Oct 05 #Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 #Python
python实现计算资源图标crc值的方法
Oct 05 #Python
python求crc32值的方法
Oct 05 #Python
Python获取文件ssdeep值的方法
Oct 05 #Python
python获取Linux下文件版本信息、公司名和产品名的方法
Oct 05 #Python
python获取文件版本信息、公司名和产品名的方法
Oct 05 #Python
You might like
phpMyAdmin2.11.6安装配置方法
2008/08/24 PHP
php UBB 解析实现代码
2011/11/27 PHP
thinkphp实现like模糊查询实例
2014/10/29 PHP
php使用redis的几种常见操作方式和用法示例
2020/02/20 PHP
Apache站点配置SSL强制跳转443
2021/03/09 Servers
Ext javascript建立超链接,进行事件处理的实现方法
2009/03/22 Javascript
json 入门基础教程 推荐
2009/10/31 Javascript
JQuery实现动态适时改变字体颜色的方法
2015/03/10 Javascript
使用node.js搭建服务器
2017/05/20 Javascript
AngularJS实现表格的增删改查(仅限前端)
2017/07/04 Javascript
vue loadmore组件上拉加载更多功能示例代码
2017/07/19 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
Fundebug支持监控微信小程序HTTP请求错误的方法
2019/02/21 Javascript
详解Vue调用手机相机和相册以及上传
2019/05/05 Javascript
angular多语言配置详解
2019/05/16 Javascript
layui 上传文件_批量导入数据UI的方法
2019/09/23 Javascript
vue项目出现页面空白的解决方案
2019/10/31 Javascript
JS实现表单中点击小眼睛显示隐藏密码框中的密码
2020/04/13 Javascript
jQuery实现简单QQ聊天框
2020/08/27 jQuery
基于vue项目设置resolves.alias: '@'路径并适配webstorm
2020/12/02 Vue.js
python利用标准库如何获取本地IP示例详解
2017/11/01 Python
Python 实现引用其他.py文件中的类和类的方法
2018/04/29 Python
Django添加KindEditor富文本编辑器的使用
2018/10/24 Python
python pandas库的安装和创建
2019/01/10 Python
Tensorflow实现部分参数梯度更新操作
2020/01/23 Python
CSS3教程(2):网页边框半径和网页圆角
2009/04/02 HTML / CSS
CSS3实现头像旋转效果
2017/03/13 HTML / CSS
div或img图片高度随宽度自适应的方法
2020/02/06 HTML / CSS
美国折扣宠物药房:Total Pet Supply
2018/05/27 全球购物
银行办公室岗位职责
2014/03/10 职场文书
护士找工作求职信
2014/07/02 职场文书
出资证明书范本(标准版)
2014/09/24 职场文书
高三复习计划
2015/01/19 职场文书
新闻发布会新闻稿
2015/07/17 职场文书
安全生产隐患排查制度
2015/08/05 职场文书
Mysql多层子查询示例代码(收藏夹案例)
2022/03/31 MySQL