python解析xml文件操作实例

本文实例讲述了python解析xml文件操作的实现方法。分享给大家供大家参考。具体方法如下:

xml文件内容如下:

       sample xml thing            ma                xiaoju              Springs Widgets, Inc.            First            I think widgets are greate.You should buy lots of them forom                Spirngy Widgts, Inc       

登录后复制

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程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2291518.html

(0)
上一篇 2025年2月28日 01:06:35
下一篇 2025年2月27日 23:55:36

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论