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实现登陆知乎获得个人收藏并保存为word文件
Mar 16 Python
Python中处理字符串的相关的len()方法的使用简介
May 19 Python
python 简单的多线程链接实现代码
Aug 28 Python
python版简单工厂模式
Oct 16 Python
Django发送邮件和itsdangerous模块的配合使用解析
Aug 10 Python
python面向对象 反射原理解析
Aug 12 Python
python实现微信小程序用户登录、模板推送
Aug 28 Python
Python中用pyinstaller打包时的图标问题及解决方法
Feb 17 Python
Tensorflow实现将标签变为one-hot形式
May 22 Python
Pytorch转onnx、torchscript方式
May 25 Python
Python 2.6.6升级到Python2.7.15的详细步骤
Dec 14 Python
Python requests库参数提交的注意事项总结
Mar 29 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远程采集图片详细教程
2014/07/01 PHP
PHP+redis实现微博的拉模型案例详解
2019/07/10 PHP
完整显示当前日期和时间的JS代码
2007/09/17 Javascript
jQuery 页面载入进度条实现代码
2009/02/08 Javascript
&amp;lt;script defer&amp;gt; defer 是什么意思
2009/05/10 Javascript
CSS和JS标签style属性对照表(方便js开发的朋友)
2010/11/11 Javascript
最佳JS代码编写的14条技巧
2011/01/09 Javascript
JQuery onload、ready概念介绍及使用方法
2013/04/27 Javascript
如何让浏览器支持jquery ajax load 前进、后退功能
2014/06/12 Javascript
Node.js中npm常用命令大全
2016/06/09 Javascript
jQuery插件HighCharts绘制简单2D柱状图效果示例【附demo源码】
2017/03/21 jQuery
浅谈微信页面入口文件被缓存解决方案
2018/09/29 Javascript
Js视频播放器插件Video.js使用方法详解
2020/02/04 Javascript
简介Python中用于处理字符串的center()方法
2015/05/18 Python
Python使用Beautiful Soup包编写爬虫时的一些关键点
2016/01/20 Python
Python正则表达式使用经典实例
2016/06/21 Python
Python处理JSON数据并生成条形图
2016/08/05 Python
Python设计模式之抽象工厂模式
2016/08/25 Python
django框架如何集成celery进行开发
2017/05/24 Python
Python操作mongodb数据库进行模糊查询操作示例
2018/06/09 Python
在python下读取并展示raw格式的图片实例
2019/01/24 Python
Python 装饰器原理、定义与用法详解
2019/12/07 Python
python调用摄像头的示例代码
2020/09/28 Python
pycharm 复制代码出现空格的解决方式
2021/01/15 Python
HTML5之语义标签介绍
2016/07/07 HTML / CSS
详解HTML5 window.postMessage与跨域
2017/05/11 HTML / CSS
Under Armour西班牙官网:美国知名的高端功能性运动品牌
2018/12/12 全球购物
后进生转化工作制度
2014/01/17 职场文书
测试工程师程序员求职信范文
2014/02/20 职场文书
毕业自我鉴定书
2014/03/24 职场文书
王金山在党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
2014超市收银员工作总结
2014/11/13 职场文书
2014年基建工作总结
2014/12/12 职场文书
《多彩的民间艺术》教学反思
2016/02/16 职场文书
上帝为你开了一扇窗之Tkinter常用函数详解
2021/06/02 Python
关于@OnetoMany关系映射的排序问题,使用注解@OrderBy
2021/12/06 Java/Android