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和perl实现批量对目录下电子书文件重命名的代码分享
Nov 21 Python
简单介绍Python中的round()方法
May 15 Python
浅析Python中将单词首字母大写的capitalize()方法
May 18 Python
python os用法总结
Jun 08 Python
Python实现注册、登录小程序功能
Sep 21 Python
python 输入一个数n,求n个数求乘或求和的实例
Nov 13 Python
pandas读取csv文件提示不存在的解决方法及原因分析
Apr 21 Python
Python基于QQ邮箱实现SSL发送
Apr 26 Python
解决tensorflow/keras时出现数组维度不匹配问题
Jun 29 Python
python中selenium库的基本使用详解
Jul 31 Python
简单谈谈Python面向对象的相关知识
Jun 28 Python
python创建字典及相关管理操作
Apr 13 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
用Socket发送电子邮件(利用需要验证的SMTP服务器)
2006/10/09 PHP
php缓冲 output_buffering的使用详解
2013/06/13 PHP
php除数取整示例
2014/04/24 PHP
PHP面向对象学习之parent::关键字
2017/01/18 PHP
php实现文章评论系统
2019/02/18 PHP
PHP Swoole异步读取、写入文件操作示例
2019/10/24 PHP
详解php反序列化
2020/06/10 PHP
javascript实现轮显新闻标题链接
2007/08/13 Javascript
将函数的实际参数转换成数组的方法
2010/01/25 Javascript
form表单只提交数据而不进行页面跳转的解决方案
2013/09/18 Javascript
jQuery中数据缓存$.data的用法及源码完全解析
2016/04/29 Javascript
浅谈JavaScript的函数及作用域
2016/12/30 Javascript
Node.js对MongoDB数据库实现模糊查询的方法
2017/05/03 Javascript
深入理解Webpack 中路径的配置
2017/06/17 Javascript
JS+canvas实现的五子棋游戏【人机大战版】
2017/07/19 Javascript
JS立即执行函数功能与用法分析
2019/01/15 Javascript
javascript json对象小技巧之键名作为变量用法分析
2019/11/11 Javascript
js实现小时钟效果
2020/03/25 Javascript
微信小程序转化为uni-app项目的方法示例
2020/05/22 Javascript
Python实现按当前日期(年、月、日)创建多级目录的方法
2018/04/26 Python
在python中将字符串转为json对象并取值的方法
2018/12/31 Python
python paramiko远程服务器终端操作过程解析
2019/12/14 Python
Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】
2020/05/29 Python
深入了解canvas在移动端绘制模糊的问题解决
2019/04/30 HTML / CSS
HTML中meta标签及Keywords
2020/04/15 HTML / CSS
英国最大的网上药品商店:Chemist Direct
2017/12/16 全球购物
美国保健品专家:Life Extension
2018/05/04 全球购物
普天C++笔试题
2016/03/20 面试题
行政人员工作职责
2013/12/05 职场文书
服务行业口号
2014/06/11 职场文书
副总经理岗位职责
2015/02/02 职场文书
员工家属慰问信
2015/03/24 职场文书
党支部考察鉴定意见
2015/06/02 职场文书
幼儿园中班教学反思
2016/03/03 职场文书
代码解析React中setState同步和异步问题
2021/06/03 Javascript
ConstraintValidator类如何实现自定义注解校验前端传参
2021/06/18 Java/Android