python对xml封装 dom处理

#!/usr/bin/python
#coding=utf8
from xml.dom import minidom
class ChildIndexOutofBoundsException(Exception):
	pass
class DOMXmlUtil:
	def __init__(self):
		pass
	def readXmlString(self,data):
		self.dom=minidom.parseString(data,"UTF-8")
		self.root=self.dom.firstChild
	def readXmlFile(self,fp):
		self.dom=minidom.parse(fp)
		self.root=self.dom.firstChild
	def getRootElement(self):
		return self.root 
		
class XmlNode:
	def __init__(self,elem):
		self.elem=elem
		
		
	def getChildByIndex(self,index):
		children=self.elem.childNodes
		children_elem=[]
		for child in children:
			if child.nodeType==minidom.Node.ELEMENT_NODE:
				children_elem.append(child)
		if index>len(children_elem):
			raise ChildIndexOutofBoundsException
		return XmlNode(children_elem[index])
		
	def getChildByName(self,name):
		children=self.elem.childNodes
		for child in children:
			if child.nodeType==minidom.Node.ELEMENT_NODE:
				if child.nodeName==name:
					return XmlNode(child)
		return None
		
	def getAttributeByIndex(self,index):
		attributes=self.elem.attributes
		keys=attributes.keys()
		if index>len(keys):
			raise ChildIndexOutofBoundsException
		return Attribute(keys[index],attributes.getNamedItem(keys[index]).value)
			
	def getAttributeByName(self,name):
		attributes=self.elem.attributes
		keys=attributes.keys()
		for each in keys:
			if each==name:
				return Attribute(name,attributes.getNamedItem(name).value)
		return None
		
	def getName(self):
		return self.elem.nodeName 
		
	def getValue(self):
		if self.elem.childNodes.length==0:
			return ""
		elif self.elem.childNodes.length==1:
			return self.elem.firstChild.nodeValue
		else:
			return None
		
	
	def getChildList(self):
		children=self.elem.childNodes
		childList=[]
		for child in children:
			if child.nodeType==minidom.Node.ELEMENT_NODE:
				childList.append(XmlNode(child))
		return childList
		
	def getAttributeList(self):
		attributes=self.elem.attributes
		attrList=[]
		keys=attributes.keys()
		for key in keys:
			attrList.append(Attribute(key,attributes.getNamedItem(key).value))
		return attrList
		

class Attribute:
	def __init__(self,name,value):
		self.name=name
		self.value=value
	def getName(self):
		return self.name
	def getValue(self):
		return self.value

def main():
	#test1();
	test2();	
	#test3()
	
def test1():
	dom=DOMXmlUtil()
	dom.readXmlFile("GetCurrencies.xml")
	n=XmlNode(dom.getRootElement())
	curs=n.getChildByName("GetCurrencies").getChildByName("CurrencyList").getChildList()
	print len(curs)
	for each in curs:
		print each.getChildByName("Name").getValue()+"\t"+each.getChildByName("Code").getValue()+"\t"+each.getChildByName("UsdRate").getValue()
	
	
def test2():
	dom=DOMXmlUtil()
	dom.readXmlFile("demo.xml")
	root=XmlNode(dom.getRootElement())
	attrs=root.getAttributeList()
	for each in attrs:
		print each.getName()+"\t"+each.getValue()
	
	stus=root.getChildList()
	for each in stus:
		print each.getAttributeByIndex(0).getValue()+"\t"+each.getChildByName("Name").getValue()+"\t"+each.getChildByIndex(1).getValue()+"\t"+each.getChildByName("Gender").getValue()	
	
	
def test3():
	dom=DOMXmlUtil()
	dom.readXmlFile("/home/jemy/Temp/GetCitiesData.xml")
	root=XmlNode(dom.getRootElement())
	cityList=root.getChildByName("GetCitiesData").getChildByName("CityList").getChildList()
	for city in cityList:
		print city.getChildByIndex(0).getValue()+"\t"+city.getChildByIndex(1).getChildByIndex(0).getValue()+"\t"+city.getChildByIndex(1).getChildByIndex(1).getValue()
	
if __name__=="__main__":
	main()

你可能感兴趣的:(exception,xml,python,import,each,attributes)