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
投稿
猜你喜欢
- 1. APScheduler简介APscheduler全称Advanced Python Scheduler作用为在指定的时间规则执行指定的
- 服务器重新启动的情况当mysqld启动时,所有的授权表内容被读进存储器并且从那时开始生效。被服务器立即应用的情况用GRANT、REVOKE或
- UDP 客户端一个使用UDP协议的客户端示例代码,用于实现连续对话。请注意,UDP是无连接协议,因此在实现连续对话时需要特别小心。以下是示例
- 写完调用天气接口的demo之后,小程序调用天气接口并且渲染在页面,顺便再调用了一下美图的接口API:美图APIurlwxml:<vie
- 一:背景引入对于一张图片,怎样修改局部像素值?二:利用Tensor方法比如输入全零tensor,可认为为黑 * 片>>> n
- 本文介绍python如何进行截图保存的几种方法,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图。结合Python其它模块如time
- 本文实例讲述了Python多线程下载文件的方法。分享给大家供大家参考。具体实现方法如下:import httplibimport urlli
- 目前市面上成熟的CMS产品有很多,无外乎两种解决方案:第一种就是用XSL+XML的方式来实现客户端具体数据;第二种是生成HTML或者SHTM
- 如下所示:lists = ['tom','Jack','luCy','lily
- 对shuffle=True的理解:之前不了解shuffle的实际效果,假设有数据a,b,c,d,不知道batch_size=2后打乱,具体是
- 逐步回归的基本思想是将变量逐个引入模型,每引入一个解释变量后都要进行F检验,并对已经选入的解释变量逐个进行t检验,当原来引入的解释变量由于后
- 定义一个什么都不做的函数>>> def a():... pass...>>> def printHell
- 常见的 Python 文件后缀有:py、pyc 、pyo、 pyi、pyw、 pyd、 pyx 等。本文只介绍相对常见的一些后缀名,至于一些
- 多线程多线程是个提高程序运行效率的好办法,本来要顺序执行的程序现在可以并行执行,可想而知效率要提高很多。但是多线程也不是能提高所有程序的效率
- 环境: python 2.7 + win10工具:fiddler postman 安卓模拟器首先,打开fiddler,fiddler作为ht
- 前言目前有一张tif格式的栅格影像,需要在web地图上进行展示,使用动态切片WMS的方式,渲染速度比较慢,而且大的时候会出现模糊的问题。并且
- 自动上次ymPrompt组件发布,自己就曾发现在IE8下遮罩的半透明滤镜有时无效的问题,后来也有网友提出过这个问题,但自己一直也没有太多关注
- 在asp中调用sql server的存储过程可以加快程序运行速度,本文介绍了asp使用存储过程的方法。1.调用存储过程的一般方法 先假设在s
- 如下所示:f=file('a.txt')for eachline in f: print eachline来源:https:
- 什么是POP3、SMTP和MAP?POP3是Post Office Protocol 3的简称,即邮局协议的第三个版本,他是规定怎样将个人计