python解析xml文件操作实例
作者:shichen2014 发布时间:2022-01-02 10:39:13
标签:python,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程序设计有所帮助。
0
投稿
猜你喜欢
- 在开发 图像 动画 游戏 相关的程序时, 双缓冲( double-buffer )一直是程序员常用(必用)的技巧. 当然 随着各种引擎 框架
- python函数的闭包问题(内嵌函数)>>> def func1():... print ('fun
- 软件版本:apache:Apache 2.4.6 Win64 PHP:PHP 5.5 VC11 x64 Non Thr
- 前言使用matplotlib生成gif动画的方法有很多,一般常规使用matplotlib的animation模块的FuncAnimation
- 本文实例为大家分享了PHP文件打包下载zip的具体代码,供大家参考,具体内容如下<?php//获取文件列表function list_
- 在改进SQL Server 7.0系列所实现的安全机制的过程中,Microsoft建立了一种既灵活又强大的安全管理机制,它能够对用户访问SQ
- 方法一(只有mdf没有日志文件的可以恢复) 证明有效 1.新建同名数据库。 2.把该数据库设置为脱机。 3.删除其日志文件(.LDF),不删
- DJANGO_SETTINGS_MODULE使用Django时要通知Django当前使用的是哪个配置文件。可以改变环境变量 DJANGO_S
- 这学期在学习编译原理,最近的上机作业就是做一个简单的词法分析器,在做的过程中,突然有个需求就是判断一个字符串是否为合法的标示符,因为我是用p
- 本文实例讲述了python获取从命令行输入数字的方法。分享给大家供大家参考。具体如下:#--------------------------
- asp按关键字查询XML的问题 '-------------------------------------------------
- Sql server中常用的几个数据类型: binary 固定长度的二进制数据,其最大长度为 8,000 个字节。 varbinary 可变
- 在 InnoDB中更加快速的全表扫描 一般来讲,大多数应用查询的时候都会用索引,查找很少的几行数据(主键查找或百行内的
- 在对float零值判断时往往只需要和0做==即可,所以曾经int和float都用==0来做对比,比如下方: in
- 今天是Firefox3的2008下载日(貌似北京时间是6.18的凌晨1:00),这就意味着Firefox3正式发布了。Firefox3有众多
- 本文实例为大家分享了python实现发送QQ邮件的具体代码,供大家参考,具体内容如下东西比较简单,简单讲一下,直接贴代码了,其他邮箱都类似。
- 引言膨胀与腐蚀是图像处理中两种最基本的形态学操作,膨胀将目标点融合到背景中,向外部扩展,腐蚀与膨胀意义相反,消除连通的边界,使边界向内收缩。
- 大家都知道系统存储过程是无法用工具导出的(大家可以试试 >任务>生成SQL脚本) 因为系统存储过程一般是不让开发人员修改的。 需
- 使用MySQL,安全问题不能不注意。以下是MySQL提示的23个注意事项:1.如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就
- array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组。 当几个数组元素的值相等时