python解析xml文件操作實(shí)例
更新時(shí)間:2014年10月05日 15:07:42 投稿:shichen2014
這篇文章主要介紹了python解析xml文件操作實(shí)例,是操作XML文件的常見技巧,需要的朋友可以參考下
本文實(shí)例講述了python解析xml文件操作的實(shí)現(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程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于Python實(shí)現(xiàn)音樂播放器的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了如何利用Python編寫簡易的音樂播放器,文中的示例代碼講解詳細(xì),具有一的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04Python socket實(shí)現(xiàn)簡單聊天室
這篇文章主要為大家詳細(xì)介紹了Python socket實(shí)現(xiàn)簡單聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python如何求數(shù)組連續(xù)最大和的示例代碼
這篇文章主要介紹了python如何求數(shù)組連續(xù)最大和的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Python MySQLdb 執(zhí)行sql語句時(shí)的參數(shù)傳遞方式
這篇文章主要介紹了Python MySQLdb 執(zhí)行sql語句時(shí)的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集
這篇文章主要介紹了Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集,本文講解了字典中常見方法列表、創(chuàng)建字典的五種方法、字典中鍵值遍歷方法等內(nèi)容,需要的朋友可以參考下2015-06-06