日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python讀取XML文檔正確應(yīng)用方式解讀

對(duì)于剛剛接觸Python的初學(xué)者來說,他們?cè)趯W(xué)習(xí)的過程中會(huì)逐漸的發(fā)現(xiàn)這一編程語言實(shí)際上一款功能強(qiáng)大應(yīng)用簡(jiǎn)單的計(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. websites= st.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)介紹。


網(wǎng)頁題目:Python讀取XML文檔正確應(yīng)用方式解讀
瀏覽路徑:http://www.5511xx.com/article/dhjcjgc.html