• 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的几种方式

相关文章

  • 使用FastCGI部署Python的Django应用的教程
  • Centos5.x下升级python到python2.7版本教程
  • Python使用scrapy采集时伪装成HTTP/1.1的方法
  • 在Django框架中运行Python应用全攻略
  • Python自定义主从分布式架构实例分析
  • Python 26进制计算实现方法
  • 在Django中限制已登录用户的访问的方法
  • Python中使用Inotify监控文件实例
  • python计数排序和基数排序算法实例
  • python中的reduce内建函数使用方法指南

文章分类

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

最近更新的内容

    • 使用Python操作Elasticsearch数据索引的教程
    • python的random模块及加权随机算法的python实现方法
    • python获取远程图片大小和尺寸的方法
    • python实现的希尔排序算法实例
    • Python编程中字符串和列表的基本知识讲解
    • Python实现的数据结构与算法之基本搜索详解
    • 在Python的Flask框架中实现单元测试的教程
    • python使用PythonMagick将jpg图片转换成ico图片的方法
    • Python sys.argv用法实例
    • Python中使用glob和rmtree删除目录子目录及所有文件的例子

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

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