发布于 2014-10-07 23:37:35 | 171 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

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) 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务