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 过滤字符串的技巧,map与itertools.imap
Sep 06 Python
Python中的面向对象编程详解(下)
Apr 13 Python
python&amp;MongoDB爬取图书馆借阅记录
Feb 05 Python
Django验证码的生成与使用示例
May 20 Python
Python实现读取机器硬件信息的方法示例
Jun 09 Python
python requests指定出口ip的例子
Jul 25 Python
python global关键字的用法详解
Sep 05 Python
python十进制转二进制的详解
Feb 07 Python
python中的 zip函数详解及用法举例
Feb 16 Python
python获取天气接口给指定微信好友发天气预报
Dec 28 Python
教你用Python matplotlib库制作简单的动画
Jun 11 Python
python 远程执行命令的详细代码
Feb 15 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
php环境下利用session防止页面重复刷新的具体实现
2014/01/09 PHP
JQUERY CHECKBOX全选,取消全选,反选方法三
2008/08/30 Javascript
jquery $.ajax各个事件执行顺序
2010/10/15 Javascript
Grid得到选择行数据的方法总结
2011/01/17 Javascript
自定义右键属性覆盖浏览器默认右键行为实现代码
2013/02/02 Javascript
关于js数组去重的问题小结
2014/01/24 Javascript
使用GruntJS构建Web程序之Tasks(任务)篇
2014/06/06 Javascript
PHP结合jQuery实现的评论顶、踩功能
2015/07/22 Javascript
jQuery实现向下滑出的二级菜单效果实例
2015/08/22 Javascript
jQuery实现的网页左侧在线客服效果代码
2015/10/23 Javascript
javascript实现九宫格相加数值相等
2020/05/28 Javascript
javascript基础知识
2016/06/07 Javascript
PHP抓取HTTPS内容和错误处理的方法
2016/09/30 Javascript
微信小程序 加载 app-service.js 错误解决方法
2016/10/12 Javascript
AngularJS实现页面定时刷新
2017/03/14 Javascript
js如何获取网页所有图片
2017/05/12 Javascript
JS运动特效之链式运动分析
2018/01/24 Javascript
基于JS实现html中placeholder属性提示文字效果示例
2018/04/19 Javascript
详解javascript中的变量提升和函数提升
2018/05/24 Javascript
JS/HTML5游戏常用算法之路径搜索算法 随机迷宫算法详解【普里姆算法】
2018/12/13 Javascript
基于element-ui封装表单金额输入框的方法示例
2021/01/06 Javascript
Python开发的单词频率统计工具wordsworth使用方法
2014/06/25 Python
Python Tkinter基础控件用法
2014/09/03 Python
python 删除大文件中的某一行(最有效率的方法)
2017/08/19 Python
Python如何读写CSV文件
2020/08/13 Python
python 监控服务器是否有人远程登录(详细思路+代码)
2020/12/18 Python
Haggar官网:美国男装品牌
2020/02/16 全球购物
体育纪念品、亲笔签名的体育收藏品:Steiner Sports
2020/07/31 全球购物
院领导写的就业推荐信
2014/03/09 职场文书
机关干部三严三实心得体会
2014/10/13 职场文书
当幸福来敲门观后感
2015/06/01 职场文书
2016年清明节期间群众祭祀活动工作总结
2016/04/01 职场文书
导游词之藏龙百瀑景区
2019/12/30 职场文书
nginx服务器的下载安装与使用详解
2021/08/02 Servers
python模板入门教程之flask Jinja
2022/04/11 Python
vue实现简易音乐播放器
2022/08/14 Vue.js