欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python解析xml文件操作實(shí)例

 更新時(shí)間:2014年10月05日 15:07:42   投稿:shichen2014  
這篇文章主要介紹了python解析xml文件操作實(shí)例,是操作XML文件的常見技巧,需要的朋友可以參考下

本文實(shí)例講述了python解析xml文件操作的實(shí)現(xiàn)方法。分享給大家供大家參考。具體方法如下:

xml文件內(nèi)容如下:

<?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程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 基于Python實(shí)現(xiàn)音樂播放器的實(shí)現(xiàn)示例代碼

    基于Python實(shí)現(xiàn)音樂播放器的實(shí)現(xiàn)示例代碼

    這篇文章主要介紹了如何利用Python編寫簡易的音樂播放器,文中的示例代碼講解詳細(xì),具有一的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • Python之os模塊案例詳解

    Python之os模塊案例詳解

    這篇文章主要介紹了Python之os模塊案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 通過python將大量文件按修改時(shí)間分類的方法

    通過python將大量文件按修改時(shí)間分類的方法

    今天小編就為大家分享一篇通過python將大量文件按修改時(shí)間分類的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python socket實(shí)現(xiàn)簡單聊天室

    Python socket實(shí)現(xiàn)簡單聊天室

    這篇文章主要為大家詳細(xì)介紹了Python socket實(shí)現(xiàn)簡單聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python賦值操作方法分享

    python賦值操作方法分享

    python的賦值操作有幾種類型,下面我們分別給大家說下,需要的朋友可以參考下
    2013-03-03
  • python如何求數(shù)組連續(xù)最大和的示例代碼

    python如何求數(shù)組連續(xù)最大和的示例代碼

    這篇文章主要介紹了python如何求數(shù)組連續(xù)最大和的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 如何使用python請求傳遞csrftoken

    如何使用python請求傳遞csrftoken

    這篇文章主要介紹了如何使用python請求傳遞csrftoken問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python MySQLdb 執(zhí)行sql語句時(shí)的參數(shù)傳遞方式

    Python MySQLdb 執(zhí)行sql語句時(shí)的參數(shù)傳遞方式

    這篇文章主要介紹了Python MySQLdb 執(zhí)行sql語句時(shí)的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集

    Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集

    這篇文章主要介紹了Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集,本文講解了字典中常見方法列表、創(chuàng)建字典的五種方法、字典中鍵值遍歷方法等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • python模塊的安裝以及安裝失敗的解決方法

    python模塊的安裝以及安裝失敗的解決方法

    Python 模塊(Module),是一個(gè) Python 文件,以 .py 結(jié)尾,包含了 Python 對象定義和Python語句。模塊讓你能夠有邏輯地組織你的 Python 代碼段。把相關(guān)的代碼分配到一個(gè)模塊里能讓你的代碼更好用,更易懂。模塊能定義函數(shù),類和變量,模塊里也能包含可執(zhí)行的代碼
    2021-11-11

最新評論