'''
Created on 2012-1-10
Scan a xml doc
@author: xiaojay
'''
from xml.dom import minidom , Node
class bookscanner:
def __init__(self,doc):
for child in doc.childNodes :
if child.nodeType == Node.ELEMENT_NODE \
and child.tagName == "book" :
bookid = child.getAttribute("id")
print "*"*20
print "Book id : " , bookid
self.handle_book(child)
def handle_book(self,node):
for child in node.childNodes :
if child.nodeType == Node.ELEMENT_NODE :
if child.tagName == "title":
print "Title : " , self.getText(child.firstChild)
if child.tagName == "author":
self.handle_author(child)
if child.tagName == "pubdate":
print "Pubdate : " , self.getText(child.firstChild)
def getText(self,node):
if node.nodeType == Node.TEXT_NODE :
return node.nodeValue
else: return ""
def handle_author(self,node):
author = node.firstChild
for child in author.childNodes:
if child.nodeType == Node.ELEMENT_NODE:
if child.tagName == "firstname" :
print "Firstname : ", self.getText(child.firstChild)
if child.tagName == "lastname" :
print "Lastname : " , self.getText(child.firstChild)
doc = minidom.parse("book.xml")
for child in doc.childNodes :
if child.nodeType == Node.COMMENT_NODE:
print "Conment : " , child.nodeValue
if child.nodeType == Node.ELEMENT_NODE:
bookscanner(child)
输出结果:
Conment : This is a simple xml.
********************
Book id : 1001
Title : An apple
Firstname : Peter
Lastname : Zhang
Pubdate : 2012-1-12
********************
Book id : 1002
Title : Love
Firstname : Mike
Lastname : Li
Pubdate : 2012-1-10
********************
Book id : 1003
Title : Steve.Jobs
Firstname : Tom
Lastname : Wang
Pubdate : 2012-1-19
********************
Book id : 1004
Title : Harry Potter
Firstname : Peter
Lastname : Chen
Pubdate : 2012-11-11