关于asp多层体系架构的思考(3)

这节讲common中,xmlparse.class.asp,即xml解析类的实现
1)实现xml节点的查找,添加,删除,修改
<%
Class xmlparser
    
    
Private m_async
    
Private m_xmlpath
    
Private xmldoc
    
Private fso

    
Sub class_initialize
        
Set xmldoc=server.createobject("microsoft.xmldom")
        
Set fso=CreateObject("Scripting.FileSystemObject")
    
End Sub 

    
Sub class_teminate
        
Set xmldoc=Nothing 
        
Set fso=Nothing 
    
End Sub 

    
Public Property Get xmlpath
        xmlpath
=m_xmlpath
    
End Property 

    
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 

    
Public Property Get async
        async
=m_async
    
End Property 

    
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 

    
Public Function selectXmlNodeText(elementname) 
        elementname
="//"&elementname 
        temp
=xmldoc.selectSingleNode(elementname).text 
        selectXmlNodeText
= server.htmlencode(temp) 
    
End Function

    
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 

    
Public Function updateXmlNodeText(elementname,newelementtext) 
        
dim element 
        
set element=xmldoc.selectSingleNode("//"&elementname) 
        element.text
=newelementtext 
    
end function 

    
Public Function deleteXmlNodeText(elementname) 
        xmldoc.selectSingleNode(
"//"&elementname).text ="" 
    
end function 

    
Public Function createElement(ByVal e)
        
Dim root 
        
Set root=xmldoc.createElement(e)
        xmldoc.appendchild(root)
    
End Function 

    
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 

    
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 

    
Public Function updateAttribute(ByVal node,ByVal key,ByVal value)
        
Dim thisnode
        
Set thisnode=xmldoc.selectSingleNode("//"&node)
        thisnode.GetAttributeNode(key).NodeValue
=value
    
End Function 

    
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 

    
Public Function getAttributeCollection(ByVal key)
        
Dim attrs
        
Set attrs=xmldoc.documentElement.SelectSingleNode("//"&key).attributes
        
Set getAttributeCollection=attrs
    
End Function 

    
Public Function save(ByVal savepath)
        
If savepath&""="" Then 
            xmldoc.save(m_xmlpath)
        
Else
            xmldoc.save(server.mappath(savepath))
        
End If 
    
End Function 
End Class


'====================================================================================================
    '取出一个节点的字节点集合
    '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

恳请批评和指正
posted @ 2008-01-10 12:01  落叶潇潇雨  阅读(428)  评论(2编辑  收藏  举报