python中的xml.dom的使用

 python中的xml.dom模块使用的就是传统的dom解析api和方法。所以也就不写什么了,主要就是练习敲敲代码,继续熟悉python。本文通过xml.dom.minidom创建一个xml文档,然后再解析出来,用以熟悉相关接口方法的使用。

创建一个xml文档:

?
'''
Created on 2012-1-10
Create a xml document
@author: xiaojay
'''
  
from xml.dom import minidom
  
doc = minidom.Document()
doc.appendChild(doc.createComment( "This is a simple xml." ))
booklist = doc.createElement( "booklist" )
doc.appendChild(booklist)
  
def addBook(newbook):
     book = doc.createElement( "book" )
     book.setAttribute( "id" , newbook[ "id" ])
      
     title = doc.createElement( "title" )
     title.appendChild(doc.createTextNode(newbook[ "title" ]))
     book.appendChild(title)
      
     author = doc.createElement( "author" )
     name = doc.createElement( "name" )
     firstname = doc.createElement( "firstname" )
     firstname.appendChild(doc.createTextNode(newbook[ "firstname" ]))
     lastname = doc.createElement( "lastname" )
     lastname.appendChild(doc.createTextNode(newbook[ "lastname" ]))
     name.appendChild(firstname)
     name.appendChild(lastname)
     author.appendChild(name)
     book.appendChild(author)
      
     pubdate = doc.createElement( "pubdate" )
     pubdate.appendChild(doc.createTextNode(newbook[ "pubdate" ]))
     book.appendChild(pubdate)
      
     booklist.appendChild(book)
  
addBook({ "id" : "1001" , "title" : "An apple" , "firstname" : "Peter" , "lastname" : "Zhang" , "pubdate" : "2012-1-12" })
addBook({ "id" : "1002" , "title" : "Love" , "firstname" : "Mike" , "lastname" : "Li" , "pubdate" : "2012-1-10" })
addBook({ "id" : "1003" , "title" : "Steve.Jobs" , "firstname" : "Tom" , "lastname" : "Wang" , "pubdate" : "2012-1-19" })
addBook({ "id" : "1004" , "title" : "Harry Potter" , "firstname" : "Peter" , "lastname" : "Chen" , "pubdate" : "2012-11-11" })
  
f = file ( "book.xml" , "w" )
doc.writexml(f)
f.close()

  通过doc.toprettyxml(indent, newl, encoding)方法可以优雅显示xml文档,但是要避免直接写入文本,否则会给解析带来麻烦,尽量使用自带的writexml方法。

生成的文档内容:

<?xml version="1.0" ?>
<!--This is a simple xml.-->
<booklist>
  <book id="1001">
    <title>
    An apple
    </title>
  <author>
    <name>
      <firstname>
      Peter
      </firstname>
      <lastname>
      Zhang
      </lastname>
    </name>
  </author>
  <pubdate>
  2012-1-12
  </pubdate>
  </book>

 .................
</booklist>

 

解析该xml文档:

?
'''
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值