asp操作XML基本函数

XML是常用的数据交换格式,也可以把XML文件当作数据库使用,十分方便快捷。ASP操作XML主要通过XMLDom在服务器端操作XML文件的主要方法和实现,对于小数据量,xml文件在检索更新上于ACCESS有很多优势。

下面定义了asp操作XML的基本函数:


<%
Dim XmlDoc

'打开XML文件
Function ConnectXml(Path)
    Set XmlDoc=Server.CreateObject("Microsoft.XMLDOM")
    XmlDoc.Async=False
    ConnectXml=XmlDoc.Load(Server.MapPath(Path))
End Function

'关闭XML文件
Function CloseXml(XmlDoc)
    If IsObject(XmlDoc) Then
        Set XmlDoc=Nothing
    End If
End Function

'获取某个节点的元素值:节点对象、节点名称
Function GetNodeValue(SNode,EleName)
    If SNode Is Nothing Then Exit Function
    Set TmpSub=SNode.SelectSingleNode(ELeName)
    If TmpSub Is Nothing Then
        Set TmpSub=SNode.Attributes.GetNamedItem(ELeName)
        If TmpSub Is Nothing Then Exit Function
        TmpRet=TmpSub.Value
    Else
        TmpRet=TmpSub.Text
    End If
    If Not IsNull(TmpRet) Then GetNodeValue=TmpRet
    Set TmpSub=Nothing
End Function

'获取节点所在位置:节点集、节点名称、节点值,返回数值
Function GetIndexByValue(Node,EleName,EleValue)
    If Node Is Nothing Then Exit Function
    For Tmpi=0 To Node.Length-1
        EValue=GetNodeValue(Node(Tmpi),EleName)
        If EValue=EleValue Then
            GetIndexByValue=Tmpi
            Exit Function
        End If
    Next
    GetIndexByValue=-1
End Function

'添加节点或者属性
'DOM对象、节点对象、节点名或者属性名、0节点1属性、有无内容、内容、在某个节点之前插入节点
Function CreateXmlNode(XDoc,NodePos,EleName,Key,IfTxt,Text,TmpIdx)
    Select Case Key
        Case "0"
            Set SubNode=XDoc.CreateElement(EleName)
            If TmpIdx > -1 Then    '在某个节点之前插入新节点
                Set CurNode=NodePos.InsertBefore(SubNode,NodePos.ChildNodes(TmpIdx))
                If IfTxt=1 Then CurNode.text=Text
                Set CreateXmlNode=CurNode
                Set CurNode=Nothing
            Else    '
                If IfTxt=1 Then SubNode.text=Text
                NodePos.AppendChild SubNode
                Set CreateXmlNode=SubNode
            End If
            Set SubNode=Nothing
        Case "1"
            Set AttNode=XDoc.CreateAttribute(EleName)
            If IfTxt=1 Then AttNode.text=Text
            NodePos.Attributes.SetNamedItem AttNode
            Set CreateXmlNode=AttNode
            Set AttNode=Nothing
        Case Else
            
    End Select
End Function

'添加一组节点[相当于组合节点,一次只能插入一组对应节点]:父节点、XML内容、节点位置
Function JoinXmlNode(NodePos,XmlStr,TmpIdx)
    Set NewXmlDoc=Server.CreateObject("Microsoft.XMLDOM")
    NewXmlDoc.LoadXml(XmlStr)
    Set RootNewNode=NewXmlDoc.documentElement
    If TmpIdx > -1 Then
        NodePos.InsertBefore RootNewNode,NodePos.ChildNodes(TmpIdx)
    Else
        NodePos.AppendChild(RootNewNode)
    End If
    Set NewXmlDoc=Nothing
End Function

'编辑某个节点内容:节点、名称、值、节点或者属性
Function EditXmlNode(NodePos,EleName,Text,Key)
    If Key="0" Then    '节点
        Set EleNode=NodePos.SelectSingleNode(EleName)
        EleNode.text=Text
    Else    '属性
        Set EleNode=NodePos.Attributes.GetNamedItem(EleName)
        EleNode.value=Text
    End If
    Set EleNode=Nothing
End Function

'删除节点:父节点、子节点、是否单个节点
Function DelXmlNode(PNode,EleName,IfNode,Key)
    If IfNode="0" Then
        If Key="0" Then    '节点
            Set EleNode=PNode.SelectSingleNode(EleName)
            PNode.RemoveChild EleNode
            Set EleNode=Nothing
        Else    '属性
            PNode.Attributes.RemoveNamedItem(EleName)
        End If
    Else    '删除节点之内所有节点
        PNode.RemoveChild EleName
    End If
End Function
%>


[本日志由 cnfgg 于 2009-07-20 03:43 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Asp XML 函数
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.