这节讲common中,xmlparse.class.asp,即xml解析类的实现
1)实现xml节点的查找,添加,删除,修改
<%
Class xmlparser
Private m_async
Private m_xmlpath
Private xmldoc
Private fso
![](/Images/OutliningIndicators/None.gif)
Sub class_initialize
Set xmldoc=server.createobject("microsoft.xmldom")
Set fso=CreateObject("Scripting.FileSystemObject")
End Sub
![](/Images/OutliningIndicators/None.gif)
Sub class_teminate
Set xmldoc=Nothing
Set fso=Nothing
End Sub
![](/Images/OutliningIndicators/None.gif)
Public Property Get xmlpath
xmlpath=m_xmlpath
End Property
![](/Images/OutliningIndicators/None.gif)
Public Property Let xmlpath(ByVal value)
If Not fso.FileExists(server.mappath(value)) Then
response.write "文件不存在!"
response.End
Else
m_xmlpath=server.mappath(value)
End If
End Property
![](/Images/OutliningIndicators/None.gif)
Public Property Get async
async=m_async
End Property
![](/Images/OutliningIndicators/None.gif)
Public Property Let async(ByVal value)
If value=False Then
m_async=False
else
m_async=True
End If
End Property
Public Sub load()
xmldoc.async=m_async
xmldoc.load m_xmlpath
End Sub
![](/Images/OutliningIndicators/None.gif)
Public Function selectXmlNodeText(elementname)
elementname="//"&elementname
temp=xmldoc.selectSingleNode(elementname).text
selectXmlNodeText= server.htmlencode(temp)
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function insertXmlNodeText(befelementname,elementname,elementtext)
dim befelement,element
set befelement=xmldoc.selectSingleNode("//"&befelementname)
set element= xmldoc.createelement(elementname)
befelement.insertBefore element,befelement.firstchild
element.text=elementtext
end function
![](/Images/OutliningIndicators/None.gif)
Public Function updateXmlNodeText(elementname,newelementtext)
dim element
set element=xmldoc.selectSingleNode("//"&elementname)
element.text=newelementtext
end function
![](/Images/OutliningIndicators/None.gif)
Public Function deleteXmlNodeText(elementname)
xmldoc.selectSingleNode("//"&elementname).text =""
end function
![](/Images/OutliningIndicators/None.gif)
Public Function createElement(ByVal e)
Dim root
Set root=xmldoc.createElement(e)
xmldoc.appendchild(root)
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function createNode(ByVal key,ByVal value)
Dim child
Set child=xmldoc.createNode("element",key,"")
child.text=value
xmldoc.documentElement.appendchild(child)
Set createNode=child
End Function
Public Function createChildNode(ByVal parentkey,ByVal key,ByVal value)
Dim parentNode,childNode
Set parentNode=xmldoc.selectSingleNode("//"&parentkey)
Set childNode=xmldoc.createNode("element",key,"")
childNode.text=value
parentNode.appendchild(childNode)
Set createChildNode=childNode
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function appendAttribute(ByVal node,ByVal key,ByVal value)
Dim thisnode,childNode
Set thisnode=xmldoc.selectSingleNode("//"&node)
Set childNode=xmldoc.createNode("attribute",key,"")
childNode.text=value
thisnode.SetAttributeNode(childNode)
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function updateAttribute(ByVal node,ByVal key,ByVal value)
Dim thisnode
Set thisnode=xmldoc.selectSingleNode("//"&node)
thisnode.GetAttributeNode(key).NodeValue=value
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function createAttributeNode(ByVal nodeName,ByVal nodeValue,ByVal key,ByVal value)
Dim thisnode
Set thisnode=createNode(nodeName,nodeValue)
Set attr=xmldoc.createNode("attribute",key,"")
attr.text=value
thisnode.SetAttributeNode(attr)
Set createattributeNode=thisnode
End Function
Public Function getNodesCollection(ByVal key)
Dim nodes
Set nodes=xmldoc.documentElement.SelectSingleNode("//"&key).ChildNodes
Set getNodesCollection=nodes
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function getAttributeCollection(ByVal key)
Dim attrs
Set attrs=xmldoc.documentElement.SelectSingleNode("//"&key).attributes
Set getAttributeCollection=attrs
End Function
![](/Images/OutliningIndicators/None.gif)
Public Function save(ByVal savepath)
If savepath&""="" Then
xmldoc.save(m_xmlpath)
Else
xmldoc.save(server.mappath(savepath))
End If
End Function
End Class
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
'====================================================================================================
'取出一个节点的字节点集合
'set objnodes=objdom.documentElement.SelectSingleNode("//people/man").ChildNodes
'遍历这个集合
'方法1
'for each element in objnodes
' response.write element.nodename 字节点名
' response.write element.text 字节点值
'next
'方法2
'domlength=objnodes.length
'for i = 0 to domlength-1
' response.write objnodes.childnodes(i).nodename 字节点名
' response.write objnodes.childnodes(i).text 字节点值
'next
'
''取出一个节点的属性集合
'set objnodes=objdom.documentElement.SelectSingleNode("//people/man").GetAttributeNode("name").attributes
'遍历这个集合
'for each element in objnodes
' response.write element.nodename 属性名
' response.write element.nodevalue 属性值
'next
%>
在web层的调用,如下:
Dim xp
Set xp=new xmlparser
with xp
.aync=False
.xmlpath="/web/template/news/index.html"
End with
response.write xp.xmlpath
恳请批评和指正