#!/usr/bin/python
#coding=utf8
from xml.dom import minidom
class childindexoutofbound***ception(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:
if index>len(children_elem):
raise childindexoutofbound***ception
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 childindexoutofbound***ception
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:
return childlist
def getattributelist(self):
attributes=self.elem.attributes
attrlist=
keys=attributes.keys()
for key in keys:
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()
用DOM實現對XML檔案的解析
dom的 xml.dom.minidom 子模組 xml.dom.pulldom 子模組分別提供兩種形式的解析器。1.1 node 介面物件相關函式 node.childnodes,返回當前節點中包含的節點列表,這是乙個唯讀屬性。1.2 document 介面物件相關函式 document.docu...
Dom4j對xml檔案的解析
xml已經發展成一種通用的資料交換標準了,例如不同資料庫中資料的相處移植等等,所謂xml的解析包括對xml的讀和寫,解析方式也有很多,譬如,dom解析 sax解析 dom4j jdom等等,就以dom4j為例,解析方式很簡單,具體步驟如下 讀xml檔案 1 獲取到document物件 2 獲取到ro...
使用jaxp的dom方式對xml實現增刪改查
目錄 使用jaxp實現查詢指定節點內容 使用jaxp實現查詢所有指定節點的內容 使用jaxp實現查詢所有節點 使用jaxp實現新增節點 使用jaxp實現修改指定節點內容 使用jaxp實現刪除指定節點 步驟 建立解析器工廠 根據解析器工廠建立解析器 解析xml,返回document 得到所有的name...