• 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文件实例分析

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

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

本文实例讲述了python解析xml文件的方法。分享给大家供大家参考。具体如下:

python解析xml非常方便。在dive into python中也有讲解。

如果xml的结构如下:

<?xml version="1.0" encoding="utf-8"?> 
<books> 
  <book> 
    <author>zoer</author> 
    <title>think in java</title> 
    <content>this is a good book</content> 
  </book> 
  <book> 
    <author>naughty</author> 
    <title>gone with the wind</title> 
    <content>this is a good book 2</content> 
  </book> 
  <book> 
    <author>cc</author> 
    <content>this is a good book 3</content> 
  </book> 
</books>

</div>

第三个book是没有title标记的。由于不要相信代码输入,所以在代码中要做检查(比如说检查这里的有没有子标签)。

解析代码如下:

#coding=utf-8 
#parse all books 
#author:  naughty610 
#date:   2012-8-16 
import xml.dom.minidom 
dom = xml.dom.minidom.parse('C:/Users/naughty/Desktop/books.xml') 
root = dom.documentElement 
#获取每一个下一层节点 
for node in root.childNodes:
#这样取得的是root节点以下一层的节点,而不是root节点以下所有节点 
  #取所有非text节点 
  if node.nodeType == node.ELEMENT_NODE: 
    #取author字段 
    author=node.getElementsByTagName("author") 
    if len(author)>=1: 
      print author[0].childNodes[0].data 
    #取title字段 
    title=node.getElementsByTagName("title") 
    if len(title)>=1: 
      print title[0].childNodes[0].data 
    #取content字段 
    content=node.getElementsByTagName("content") 
    if len(content)>=1: 
      print content[0].childNodes[0].data 
    print "........................parting line........................"
</div>

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

</div>

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

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

相关文章

  • 用ReactJS和Python的Flask框架编写留言板的代码示例
  • python爬虫常用的模块分析
  • 全面解析Python的While循环语句的使用方法
  • Python的Flask开发框架简单上手笔记
  • Python实现截屏的函数
  • python logging类库使用例子
  • 跟老齐学Python之重回函数
  • Python类方法__init__和__del__构造、析构过程分析
  • 零基础写python爬虫之抓取糗事百科代码分享
  • python实现倒计时的示例

文章分类

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

最近更新的内容

    • python解决方案:WindowsError: [Error 2]
    • 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
    • Python中实现字符串类型与字典类型相互转换的方法
    • python去除空格和换行符的实现方法(推荐)
    • Python创建xml的方法
    • Python的Flask框架中实现登录用户的个人资料和头像的教程
    • Python的Tornado框架实现异步非阻塞访问数据库的示例
    • Python中列表、字典、元组数据结构的简单学习笔记
    • 轻松掌握python设计模式之访问者模式
    • Python时间的精准正则匹配方法分析

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

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