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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值