asp操作XML基本函数
作者:cnfgg 日期:2009-07-20
下面定义了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
%>
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags:
相关日志: