• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python解析xml文件操作实例

python解析xml文件操作实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python解析xml文件,python读取xml文件,python生成xml文件,python 修改xml文件,python处理xml文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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> 

</div>

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) 

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python处理xml文件的方法小结
  • python处理xml文件的方法小结
  • 使用Python生成XML的方法实例
  • Python存取XML的常见方法实例分析
  • Python处理XML格式数据的方法详解
  • python解析基于xml格式的日志文件
  • Python实现简单的多任务mysql转xml的方法
  • python xml解析实例详解
  • 深入解读Python解析XML的几种方式
  • 深入解读Python解析XML的几种方式

相关文章

  • Python读写Excel文件方法介绍
  • 在Python中使用lambda高效操作列表的教程
  • Python中的默认参数详解
  • Python复制目录结构脚本代码分享
  • 用Python的线程来解决生产者消费问题的示例
  • 简单介绍Python中的几种数据类型
  • python通过floor函数舍弃小数位的方法
  • Python函数中的函数(闭包)用法实例
  • python去除空格和换行符的实现方法(推荐)
  • python使用in操作符时元组和数组的区别分析

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 微信 用脚本查看是否被微信好友删除
    • python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
    • python中二维阵列的变换实例
    • Python采用raw_input读取输入值的方法
    • 在Django的session中使用User对象的方法
    • 用Python编写简单的定时器的方法
    • Python中取整的几种方法小结
    • 详解Python中映射类型的内建函数和工厂函数
    • 由浅入深讲解python中的yield与generator
    • 详细解读Python中的__init__()方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有