python解析xml文件操作实例
作者:shichen2014 时间:2022-01-02 10:39:13
本文实例讲述了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程序设计有所帮助。
标签:python,xml
0
投稿
猜你喜欢
Keras保存模型并载入模型继续训练的实现
2021-08-12 23:23:32
解决在Dreamweaver中不支持中文文件名的方法
2010-09-02 12:35:00
Python中OpenCV图像特征和harris角点检测
2023-08-04 22:32:56
在ASP编程中nothing代表什么意思?
2011-04-15 10:47:00
简单分析Python中用fork()函数生成的子进程
2021-11-24 02:48:02
Vuex简单入门
2024-05-02 16:58:26
Dreamweaver的CSS布局ul和li范例
2009-08-28 12:34:00
django 外键model的互相读取方法
2021-06-16 20:54:51
基于Python制作一款屏幕颜色提取器
2023-11-16 05:20:48
BootStrop前端框架入门教程详解
2024-04-29 13:46:10
python模块itsdangerous简单介绍
2022-03-19 14:28:28
Python实现ElGamal加密算法的示例代码
2023-05-23 15:25:58
使用Python串口实时显示数据并绘图的例子
2023-12-26 14:41:46
MySQL异常处理浅析
2024-01-17 21:47:44
Python实现将HTML转成PDF的方法分析
2023-06-19 18:59:37
python2.7+selenium2实现淘宝滑块自动认证功能
2023-06-06 14:29:34
Python实现FLV视频拼接功能
2021-09-19 02:41:48
SQL中的开窗函数详解可代替聚合函数使用
2024-01-20 08:03:24
js实现倒计时时钟的示例代码
2024-04-22 22:30:13
彻底弄清楚haslayout概念
2009-10-27 10:46:00