自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python讀取XML文檔正確應(yīng)用方式解讀

開發(fā) 后端
Python讀取XML文檔的相關(guān)操作在實(shí)際編程中是一個(gè)比較基礎(chǔ)的應(yīng)用技術(shù)。我們?cè)趯W(xué)習(xí)的過程中需要對(duì)這一技術(shù)有一個(gè)熟練的掌握。

對(duì)于剛剛接觸Python的初學(xué)者來說,他們?cè)趯W(xué)習(xí)的過程中會(huì)逐漸的發(fā)現(xiàn)這一編程語言實(shí)際上一款功能強(qiáng)大應(yīng)用簡單的計(jì)算機(jī)程序語言。我們今天將會(huì)為大家詳細(xì)介紹一下有關(guān)Python讀取XML文檔的相關(guān)應(yīng)用方式。

最近做一個(gè)小功能,里邊包含Python讀取XML文檔的功能,封裝了一個(gè)讀取類,包括讀取xml中所有數(shù)據(jù),返回list集合;根據(jù)***節(jié)點(diǎn)值讀取該節(jié)點(diǎn)及子節(jié)點(diǎn)的值

  1. from xml.dom.minidom import parse,parseString  
  2. class XmlConfig:   
  3. def __init__(self,path):   
  4. selfself.xmlData=self.GetXml(path)  
  5. def GetText(self,nodelist):  
  6. r="" 
  7. for nxd in nd.childNodes:   
  8. rr=r+nxd.nodeValue  
  9. return r  
  10. ##獲取xml所有數(shù)據(jù)  
  11. def GetXml(self,path):  
  12. doc1=parse(path)  
  13. st=doc1.firstChild  
  14. websitesst.childNodes  
  15. lstList=[]  
  16. for sw in websites:   
  17. if sw.nodeType==sw.ELEMENT_NODE :   
  18. lsty=[]  
  19. for nd in sw.childNodes:   
  20. if nd.nodeType==nd.ELEMENT_NODE:  
  21. ndndName= nd.nodeName  
  22. ndndValue= nd.firstChild.data  
  23. b=(ndName,ndValue)  
  24. lsty.append(b)   
  25. lstList.append(lsty)  
  26. return lstList  
  27. ##獲取單個(gè)節(jié)點(diǎn)及子節(jié)點(diǎn)值   
  28. def GetSingle(self,siteName):  
  29. for item in self.xmlData:  
  30. for k,v in item:  
  31. if v==siteName:   
  32. return item  
  33. ##獲取單個(gè)節(jié)點(diǎn)及子節(jié)點(diǎn)值   
  34. def GetSingleDict(self,siteName):  
  35. lst=self.GetSingle(siteName)  
  36. dic1={}  
  37. if len(lst)>0:  
  38. for item in lst:   
  39. dic1[item[0]]=item[1]  
  40. return dic1 

xml文檔

  1. < ?xml version="1.0" encoding="UTF-8"?> 
  2. < Site> 
  3. < WebSites> 
  4. < website>http://www.xxx.net< /website> 
  5. < loginurl>http:///www.xxx.net/login.php< /loginurl> 
  6. < username>uname=xxx< /username> 
  7. < passwd>pass=123456< /passwd> 
  8. < other>< ![CDATA[r=5&remember=0&ur=xxx]]>< /other> 
  9. < config>WebSite.ini< /config> 
  10. < configname>XXX< /configname> 
  11. < /WebSites> 
  12. < WebSites> 
  13. < website>http://www.xxx.com< /website> 
  14. < loginurl>http:///www.xxx.com/login.php< /loginurl> 
  15. < username>uname=xxx< /username> 
  16. < passwd>pass=123456< /passwd> 
  17. < other>< ![CDATA[r=5&remember=0&ur=xxx]]>< /other> 
  18. < config>WebSite.ini< /config> 
  19. < configname>XXX< /configname> 
  20. < /WebSites> 
  21. < /Site> 

Python讀取XML文檔的調(diào)用:

  1. if __name__=="__main__":  
  2. f=XmlConfig()  
  3. print f.xmlData 

以上就是對(duì)Python讀取XML文檔的相關(guān)介紹。

【編輯推薦】

  1. Python操作注冊(cè)表相關(guān)步驟詳細(xì)介紹
  2. Python操作Sqlite正確實(shí)現(xiàn)方法解析
  3. Python字符基本操作技巧分享
  4. Python HTTP操作基本應(yīng)用方式簡述
  5. Python PAMIE模塊實(shí)現(xiàn)IE自動(dòng)化
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-03-05 15:01:29

Python解析XML

2010-03-04 13:30:11

Python file

2010-03-03 17:58:16

Python同步隊(duì)列

2010-02-22 14:09:08

WCF Dispose

2010-03-04 14:51:21

Python Conf

2010-02-05 14:12:46

C++聲明放置

2010-01-25 11:29:33

Android計(jì)時(shí)器

2021-07-02 16:13:01

區(qū)塊鏈金融數(shù)據(jù)庫

2010-03-04 09:40:52

Python Clas

2009-12-07 14:38:14

PHP foreach

2010-05-28 15:16:40

MySQL 資源

2010-02-22 10:42:12

WCF Stream

2010-03-04 14:02:51

Python大小寫

2010-05-25 18:01:35

linux MySQL

2010-05-28 19:20:36

MySQL mysql

2010-02-05 17:49:24

C++常量引用

2010-07-21 10:50:48

SQL Server存

2010-07-26 17:43:34

SQL Server

2010-02-01 15:26:44

C++ inline函

2010-08-18 16:45:40

IBM DB2 Cat
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)