python解析xml文件操作實例
更新時間:2014年10月05日 15:07:42 投稿:shichen2014
這篇文章主要介紹了python解析xml文件操作實例,是操作XML文件的常見技巧,需要的朋友可以參考下
本文實例講述了python解析xml文件操作的實現(xiàn)方法。分享給大家供大家參考。具體方法如下:
xml文件內(nèi)容如下:
<?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程序設計有所幫助。
相關文章
Python3與fastdfs分布式文件系統(tǒng)如何實現(xiàn)交互
這篇文章主要介紹了Python3與fastdfs分布式文件系統(tǒng)如何實現(xiàn)交互,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
Python錯誤提示:[Errno 24] Too many open files的分析與解決
這篇文章主要給大家介紹了Python中出現(xiàn)錯誤提示:[Errno 24] Too many open files的分析與解決,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問題
今天小編就為大家分享一篇解決python中os.listdir()函數(shù)讀取文件夾下文件的亂序和排序問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python實現(xiàn)查詢某個目錄下修改時間最新的文件示例
這篇文章主要介紹了Python實現(xiàn)查詢某個目錄下修改時間最新的文件,涉及Python使用os與shutil模塊針對文件的遍歷、屬性獲取、讀寫等相關操作技巧,需要的朋友可以參考下2018-08-08
Django通用類視圖實現(xiàn)忘記密碼重置密碼功能示例
今天小編就為大家分享一篇Django通用類視圖實現(xiàn)忘記密碼重置密碼功能示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python實現(xiàn)將目錄中TXT合并成一個大TXT文件的方法
這篇文章主要介紹了Python實現(xiàn)將目錄中TXT合并成一個大TXT文件的方法,涉及Python針對目錄下文本文件的遍歷、讀取及寫入等技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

