python解析xml文件操作实例

 更新时间:2014年10月05日 15:07:42   投稿:shichen2014  
这篇文章主要介绍了python解析xml文件操作实例,是操作XML文件的常见技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Python3与fastdfs分布式文件系统如何实现交互

    Python3与fastdfs分布式文件系统如何实现交互

    这篇文章主要介绍了Python3与fastdfs分布式文件系统如何实现交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python中的hashlib模块使用实例

    python中的hashlib模块使用实例

    这篇文章主要介绍了python中的hashlib模块使用实例,hashlib是一个提供字符串加密功能的模块,包含MD5和SHA的算法,MD5和SHA是摘要算法,文中以实例代码讲解hashlib模块的基本用法,需要的朋友可以参考下
    2023-08-08
  • Python错误提示:[Errno 24] Too many open files的分析与解决

    Python错误提示:[Errno 24] Too many open files的分析与解决

    这篇文章主要给大家介绍了Python中出现错误提示:[Errno 24] Too many open files的分析与解决,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • Python中用户输入与while循环详情

    Python中用户输入与while循环详情

    这篇文章主要介绍了Python中用户输入与while循环详情,,包括如何接收用户输入并进行处理,在程序满足一定的条件时让程序一直运行,通过获取用户输入并学会控制程序在用户想要结束时退出循环,即可编写出交互式程序,下文详细内容介绍,需要的朋友可以参考一下
    2022-03-03
  • 解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题

    解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题

    今天小编就为大家分享一篇解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python实现查询某个目录下修改时间最新的文件示例

    Python实现查询某个目录下修改时间最新的文件示例

    这篇文章主要介绍了Python实现查询某个目录下修改时间最新的文件,涉及Python使用os与shutil模块针对文件的遍历、属性获取、读写等相关操作技巧,需要的朋友可以参考下
    2018-08-08
  • PyQt5+QtChart实现绘制极坐标图

    PyQt5+QtChart实现绘制极坐标图

    QChart是一个QGraphicScene中可以显示的QGraphicsWidget。本文将利用QtChart实现极坐标图的绘制,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-12-12
  • Django通用类视图实现忘记密码重置密码功能示例

    Django通用类视图实现忘记密码重置密码功能示例

    今天小编就为大家分享一篇Django通用类视图实现忘记密码重置密码功能示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Python实现将目录中TXT合并成一个大TXT文件的方法

    Python实现将目录中TXT合并成一个大TXT文件的方法

    这篇文章主要介绍了Python实现将目录中TXT合并成一个大TXT文件的方法,涉及Python针对目录下文本文件的遍历、读取及写入等技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 利用Opencv实现图片的油画特效实例

    利用Opencv实现图片的油画特效实例

    这篇文章主要给大家介绍了关于利用Opencv实现图片的油画特效的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02

最新评论