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操作xml文件详细介绍
Jun 09 Python
Python 爬虫的工具列表大全
Jan 31 Python
Django模板变量如何传递给外部js调用的方法小结
Jul 24 Python
对Python字符串中的换行符和制表符介绍
May 03 Python
Python常见排序操作示例【字典、列表、指定元素等】
Aug 15 Python
Django中多种重定向方法使用详解
Jul 17 Python
Python爬虫爬取煎蛋网图片代码实例
Dec 16 Python
python DataFrame转dict字典过程详解
Dec 26 Python
解决jupyter notebook 出现In[*]的问题
Apr 13 Python
Python实现将元组中的元素作为参数传入函数的操作
Jun 05 Python
使用Python制作一个数据预处理小工具(多种操作一键完成)
Feb 07 Python
Requests什么的通通爬不了的Python超强反爬虫方案!
May 20 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
PDO的安全处理与事物处理方法
2016/10/31 PHP
Add Formatted Data to a Spreadsheet
2007/06/12 Javascript
js实现单一html页面两套css切换代码
2013/04/11 Javascript
关于jquery中全局函数each使用介绍
2013/12/10 Javascript
jQuery实现“扫码阅读”功能
2015/01/21 Javascript
JavaScript基于ajax编辑信息用法实例
2015/07/15 Javascript
js微信支付实现代码
2016/12/22 Javascript
webpack2.0配置postcss-loader的方法
2017/08/17 Javascript
JS图片延迟加载插件LazyImgv1.0用法分析【附demo源码下载】
2017/09/04 Javascript
JavaScript判断输入是否为数字类型的方法总结
2017/09/28 Javascript
js点击时关闭该范围下拉菜单之外的菜单方法
2018/01/11 Javascript
基于Axios 常用的请求方法别名(详解)
2018/03/13 Javascript
JS/HTML5游戏常用算法之碰撞检测 像素检测算法实例详解
2018/12/12 Javascript
js实现京东秒杀倒计时功能
2019/01/21 Javascript
JavaScript实现背景自动切换小案例
2019/09/27 Javascript
简单实现节流函数和防抖函数过程解析
2019/10/08 Javascript
JS实现斐波那契数列的五种方式(小结)
2020/09/09 Javascript
[01:31:03]DOTA2完美盛典全回顾 见证十五项大奖花落谁家
2017/11/28 DOTA
python 字典(dict)遍历的四种方法性能测试报告
2014/06/25 Python
python 读写文件,按行修改文件的方法
2018/07/12 Python
Python异步编程之协程任务的调度操作实例分析
2020/02/01 Python
python安装后的目录在哪里
2020/06/21 Python
python怎么删除缓存文件
2020/07/19 Python
解决python 在for循环并且pop数组的时候会跳过某些元素的问题
2020/12/11 Python
Pytorch1.5.1版本安装的方法步骤
2020/12/31 Python
python批量提取图片信息并保存的实现
2021/02/05 Python
css3让div随鼠标移动而抖动起来
2014/02/10 HTML / CSS
GLAMGLOW格莱魅美国官网:美国知名的面膜品牌
2016/12/31 全球购物
计算机专业推荐信范文
2013/11/27 职场文书
教师评优事迹材料
2014/01/10 职场文书
大学校园毕业自我鉴定
2014/01/15 职场文书
年检委托书
2014/08/30 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
2015年度物流工作总结
2015/04/30 职场文书
平凡的世界读书笔记
2015/06/25 职场文书
Nginx进程调度问题详解
2021/09/25 Servers