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.
 
 
 
  
  - From xml. dom. minidom import parse, parseString
 
  
  - Class XmlConfig:
 
  
  - Def _ init _ (self, path ):
 
  
  - Selfself. xmlData = self. GetXml (path)
 
  
  - Def GetText (self, nodelist ):
 
  
  - R = ""
 
  
  - For nxd in nd. childNodes:
 
  
  - Rr = r + nxd. nodeValue
 
  
  - Return r
 
  
  - # Retrieve all xml data
 
  
  - Def GetXml (self, path ):
 
  
  - Doc1 = parse (path)
 
  
  - St = doc1.firstChild
 
  
  - Websites = st. childNodes
 
  
  - LstList = []
 
  
  - For sw in websites:
 
  
  - If sw. nodeType = sw. ELEMENT_NODE:
 
  
  - Lsty = []
 
  
  - For nd in sw. childNodes:
 
  
  - If nd. nodeType = nd. ELEMENT_NODE:
 
  
  - NdndName = nd. nodeName
 
  
  - NdndValue = nd. firstChild. data
 
  
  - B = (ndName, ndValue)
 
  
  - Lsty. append (B)
 
  
  - LstList. append (lsty)
 
  
  - Return lstList
 
  
  - # Obtain the values of a single node and Its subnodes
 
  
  - Def GetSingle (self, siteName ):
 
  
  - For item in self. xmlData:
 
  
  - For k, v in item:
 
  
  - If v = siteName:
 
  
  - Return item
 
  
  - # Obtain the values of a single node and Its subnodes
 
  
  - Def GetSingleDict (self, siteName ):
 
  
  - Lst = self. GetSingle (siteName)
 
  
  - Dic1 = {}
 
  
  - If len (lst)> 0:
 
  
  - For item in lst:
 
  
  - Dic1 [item [0] = item [1]
 
  
  - Return dic1
 
 
 
 
Xml document
 
 
 
  
  - < ?xml version="1.0" encoding="UTF-8"?> 
 
  
  - < Site> 
 
  
  - < WebSites> 
 
  
  - < website>http://www.xxx.net< /website> 
 
  
  - < loginurl>http:///www.xxx.net/login.php< /loginurl> 
 
  
  - < username>uname=xxx< /username> 
 
  
  - < passwd>pass=123456< /passwd> 
 
  
  - < other>< ![CDATA[r=5&remember=0&ur=xxx]]>< /other> 
 
  
  - < config>WebSite.ini< /config> 
 
  
  - < configname>XXX< /configname> 
 
  
  - < /WebSites> 
 
  
  - < WebSites> 
 
  
  - < website>http://www.xxx.com< /website> 
 
  
  - < loginurl>http:///www.xxx.com/login.php< /loginurl> 
 
  
  - < username>uname=xxx< /username> 
 
  
  - < passwd>pass=123456< /passwd> 
 
  
  - < other>< ![CDATA[r=5&remember=0&ur=xxx]]>< /other> 
 
  
  - < config>WebSite.ini< /config> 
 
  
  - < configname>XXX< /configname> 
 
  
  - < /WebSites> 
 
  
  - < /Site> 
 
 
 
 
Python calls to read XML documents:
 
 
 
  
  - if __name__=="__main__":  
 
  
  - f=XmlConfig()  
 
  
  - print f.xmlData 
 
 
 
 
The above is an introduction to reading XML documents from Python.