Reading XML documents from Python

Source: Internet
Author: User

For beginners who have just been familiar with Python, they will gradually find that this programming language is actually a computer programming language with powerful functions and simple application. We will introduce in detail how to read XML documents from Python today.

Recently, I made a small function, which contains the function of reading XML documents from Python. It encapsulates a reading class, including reading all the data in xml and returning the list set; reads the value of this node and Its subnode based on the unique node value.

 
 
  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. # Retrieve all xml data
  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. # Obtain the values of a single node and Its subnodes
  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. # Obtain the values of a single node and Its subnodes
  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 document

 
 
  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 calls to read XML documents:

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

The above is an introduction to reading XML documents from Python.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.