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实现监控程序执行时间并将其写入日志的方法
Jun 30 Python
在Python中的Django框架中进行字符串翻译
Jul 27 Python
python变量不能以数字打头详解
Jul 06 Python
Python实现发送与接收邮件的方法详解
Mar 28 Python
pyqt5 实现多窗口跳转的方法
Jun 19 Python
python读写Excel表格的实例代码(简单实用)
Dec 19 Python
Python 实现自动获取种子磁力链接方式
Jan 16 Python
快速解决jupyter启动卡死的问题
Apr 10 Python
Python configparser模块应用过程解析
Aug 14 Python
python实现自动打卡的示例代码
Oct 10 Python
Django配置跨域并开发测试接口
Nov 04 Python
68行Python代码实现带难度升级的贪吃蛇
Jan 18 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的Yii框架中View视图的使用进阶
2016/03/29 PHP
php创建图像具体步骤
2017/03/13 PHP
老生常谈PHP 文件写入和读取(必看篇)
2017/05/22 PHP
关于ThinkPHP中的异常处理详解
2018/05/11 PHP
JQuery最佳实践之精妙的自定义事件
2010/08/11 Javascript
jquery实现点击TreeView文本父节点展开/折叠子节点
2013/01/10 Javascript
jquery防止重复执行动画避免页面混乱
2014/04/22 Javascript
深入分析jsonp协议原理
2015/09/26 Javascript
JS+CSS实现的竖向简洁折叠菜单效果代码
2015/10/22 Javascript
js表单验证实例讲解
2016/03/31 Javascript
bootstrap侧边栏圆点导航
2017/01/11 Javascript
socket.io实现在线群聊功能
2017/04/07 Javascript
js实现手机web图片左右滑动效果
2017/12/29 Javascript
Vue封装Swiper实现图片轮播效果
2018/02/06 Javascript
Vue组件之单向数据流的解决方法
2018/11/10 Javascript
微信小程序功能之全屏滚动效果的实现代码
2018/11/22 Javascript
JavaScript基于遍历操作实现对象深拷贝功能示例
2019/03/05 Javascript
微信小程序实现跳转的几种方式总结(推荐)
2019/04/24 Javascript
Nautil 中使用双向数据绑定的实现
2019/10/02 Javascript
js实现数字从零慢慢增加到指定数字示例
2019/11/07 Javascript
Vue2.0 $set()的正确使用详解
2020/07/28 Javascript
JavaScript快速调试的两个技巧
2020/11/04 Javascript
window下eclipse安装python插件教程
2017/04/24 Python
安装Python和pygame及相应的环境变量配置(图文教程)
2017/06/04 Python
Python实现的插入排序算法原理与用法实例分析
2017/11/22 Python
Python 12306抢火车票脚本
2018/02/07 Python
TensorFlow车牌识别完整版代码(含车牌数据集)
2019/08/05 Python
使用python快速在局域网内搭建http传输文件服务的方法
2019/11/14 Python
Python用5行代码实现批量抠图的示例代码
2020/04/14 Python
python3通过subprocess模块调用脚本并和脚本交互的操作
2020/12/05 Python
基于HTML5的WebGL经典3D虚拟机房漫游动画
2017/11/15 HTML / CSS
REISS英国官网:伦敦High Street最受欢迎品牌
2016/12/21 全球购物
美国男士和女士奢侈品折扣手表购物网站:Certified Watch Store
2018/06/13 全球购物
金士达面试非笔试
2012/03/14 面试题
化学专业大学生职业生涯规划范文
2014/09/13 职场文书
忆童年!用Python实现愤怒的小鸟游戏
2021/06/07 Python