欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python解析xml文件操作实例

程序员文章站 2023-11-04 15:54:34
本文实例讲述了python解析xml文件操作的实现方法。分享给大家供大家参考。具体方法如下: 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程序设计有所帮助。