2008年7月24日 星期四

ASP操作XML类(两种)

1、
<%
class cls_xml

private sfilename
private sxmlfile
private sobjxml

property let filename(value)
sfilename=value
end property

property get filename
filename=sfilename
end property

property get objxml
set objxml=sobjxml
end property

private sub class_initialize()
sfilename=""
sxmlfile=""
sobjxml=null
end sub

private sub class_terminate()
sobjxml.close
set sobjxml=nothing
end sub

private sub createfile()'创建xml文件
Set oPI=sobjXML.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
sobjXML.insertBefore oPI,sobjXML.childNodes(0)
savefile
end sub

public function loadfile()'读取xml文件
sxmlfile=server.MapPath(sfilename)
set sobjxml=server.CreateObject("msxml2.domdocument")
sobjxml.load(sxmlfile)
sobjxml.async=false
if sobjxml.parseError.ErrorCode = 0 then
loadfile=true
else
createfile
end if
end function

public sub addnode(byval parentnode,byval childnode,byval nodetext)'添加节点
if parentnode="" then
sobjxml.appendchild (sobjxml.createElement(childnode))
else
set child=sobjxml.createElement(childnode)
child.text=nodetext
sobjxml.selectsinglenode(parentnode).appendchild child
end if
savefile
end sub

public sub addattr(byval parentnode,byval attrtype,byval attrname,byval attrtext)'添加属性
set sattribute=sobjxml.createNode(attrtype,attrname,"")
sattribute.text=attrtext
if parentnode="" then
sobjxml.documentElement.setattributenode sattribute
else
sobjxml.documentElement.selectsinglenode(parentnode).setattributenode sattribute
end if
savefile
end sub

private function getnode(byval nodepath,byval attrname,byval attrvalue)'获得节点对象
if attrname="" then
set getnode=sobjxml.documentElement.selectsinglenode(nodepath)
else
set getnode=sobjxml.documentElement.selectsinglenode("//"&nodepath&"[@"&attrname&"='"&attrvalue&"']")
end if
end function

public function getnodename(byval nodepath,byval attrname,byval attrvalue)'获得节点名称
getnodename=getnode(nodepath,attrname,attrvalue).nodename
end function

public function getnodevalue(byval nodepath,byval attrname,byval attrvalue)'获得节点内容
getnodevalue=getnode(nodepath,attrname,attrvalue).text
end function

public sub modifynodevalue(byval nodepath,byval newtext,byval attrname,byval attrvalue)'修改节点内容
set newnode=getnode(nodepath,attrname,attrvalue)
newnode.text=newtext
savefile
end sub

public function getnodeattrname(byval nodepath,byval attrname,byval attrname_,byval attrvalue_)'获得节点属性名称
getnodeattrname=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname).nodename
end function

public function getnodeattrvalue(byval nodepath,byval attrname,byval attrname_,byval attrvalue_)'获得节点属性值
getnodeattrvalue=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname).nodevalue
end function

public sub modifynodeattrvalue(byval nodepath,byval attrname,byval newtext,byval attrname_,byval attrvalue_)'修改节点属性值
set newattr=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname)
newattr.text=newtext
savefile
end sub

public sub delnode(byval parentnodepath,byval parentnodeattrname,byval parentnodeattrvalue,byval childnodepath,byval childnodeattrname,byval childnodeattrvalue)'删除节点
if parentnodepath="" then
set parentnode=sobjxml.documentElement
else
set parentnode=getnode(parentnodepath,parentnodeattrname,parentnodeattrvalue)
end if
set childnode=getnode(childnodepath,childnodeattrname,childnodeattrvalue)
parentnode.removechild childnode
savefile
end sub

public function getchildlength(byval nodename,byval attrname,byval attrvalue)'获得节点长度
if nodename="" then
getchildlength=sobjxml.documentElement.childnodes.length
else
getchildlength=getnode(nodename,attrname,attrvalue).childnodes.length
end if
end function

private sub savefile()'保存文件
sobjxml.save(sxmlfile)
end sub

end class
%>

2、
<%'个人网站:http://blog.aq82.com
Class clsXML
 'strFile 必须是完整的路径 如:C:\XML\XMLFile.XML
 'objDoc 是 XML Object
 Private strFile, objDoc

 '类的初使化:
 Private Sub Class_Initialize()
 strFile = ""
 End Sub

 'Terminate and unload all created objects
 Private Sub Class_Terminate()
 Set objDoc = Nothing
 End Sub

 '*********************************************************************
 ' Properties
 '*********************************************************************

 'Set XML File and objDoc
 '设置XML和DOC对象
 Public Property Let File(str)
 Set objDoc = Server.CreateObject("Microsoft.XMLDOM")
 objDoc.async = False
 strFile = str
 objDoc.Load strFile
 End Property

 'Get XML File
 Public Property Get File()
 File = strFile
 End Property

 '*********************************************************************
 ' Functions
 '*********************************************************************

 '创建XML文件
 Public Function createFile(strPath, strRoot)
 Dim objFSO, objTextFile
 Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
 Set objTextFile = objFSO.CreateTextFile(strPath, True)
 objTextFile.WriteLine("<?xml version=""1.0""?>")
 objTextFile.WriteLine("<" & strRoot & "/>")
 objTextFile.Close
 Me.File = strPath
 Set objTextFile = Nothing
 Set objFSO = Nothing
 End Function

 '读取节点数据
 Public Function getField(strXPath)
 Dim objNodeList, arrResponse(), i
 Set objNodeList = objDoc.documentElement.selectNodes(strXPath)
 ReDim arrResponse(objNodeList.length)
 For i = 0 To objNodeList.length - 1
 arrResponse(i) = objNodeList.item(i).Text
 Next
 getField = arrResponse
 End Function

 '根据条件更新数据
 Public Function updateField(strXPath, strData)
 Dim objField
 For Each objField In objDoc.documentElement.selectNodes(strXPath)
 objField.Text = strData
 Next
 objDoc.Save strFile
 Set objField = Nothing
 updateField = True
 End Function

 '创建子节点
 Public Function createRootChild(strNode)
 Dim objChild
 Set objChild = objDoc.createNode(1, strNode, "")
 objDoc.documentElement.appendChild(objChild)
 objDoc.Save strFile
 Set objChild = Nothing
 End Function

 '创建节点属性值
 Public Function createRootNodeWAttr(strNode, attr, val)
 Dim objChild, objAttr
 Set objChild = objDoc.createNode(1, strNode, "")
 If IsArray(attr) And IsArray(val) Then
 If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
 Exit Function
 Else
 Dim i
 For i = LBound(attr) To UBound(attr)
 Set objAttr = objDoc.createAttribute(attr(i))
 objChild.setAttribute attr(i), val(i)
 Next
 End If
 Else
 Set objAttr = objDoc.createAttribute(attr)
 objChild.setAttribute attr, val
 End If
 objDoc.documentElement.appendChild(objChild)
 objDoc.Save strFile
 Set objChild = Nothing
 End Function

 '在一个指定的节点下创建一个子节点
 Public Function createChildNode(strXPath, strNode)
 Dim objParent, objChild
 For Each objParent In objDoc.documentElement.selectNodes(strXPath)
 Set objChild = objDoc.createNode(1, strNode, "")
 objParent.appendChild(objChild)
 Next
 objDoc.Save strFile
 Set objParent = Nothing
 Set objChild = Nothing
 End Function

 '在一个有属性值符合条件的XPath节点下创建一个子节点
 Public Function createChildNodeWAttr(strXPath, strNode, attr, val)
 Dim objParent, objChild, objAttr
 For Each objParent In objDoc.documentElement.selectNodes(strXPath)
 Set objChild = objDoc.createNode(1, strNode, "")
 If IsArray(attr) And IsArray(val) Then
 If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
 Exit Function
 Else
 Dim i
 For i = LBound(attr) To UBound(attr)
 Set objAttr = objDoc.createAttribute(attr(i))
 objChild.SetAttribute attr(i), val(i)
 Next
 End If
 Else
 Set objAttr = objDoc.createAttribute(attr)
 objChild.setAttribute attr, val
 End If
 objParent.appendChild(objChild)
 Next
 objDoc.Save strFile
 Set objParent = Nothing
 Set objChild = Nothing
 End Function

 '删除节点
 Public Function deleteNode(strXPath)
 Dim objOld
 For Each objOld In objDoc.documentElement.selectNodes(strXPath)
 objDoc.documentElement.removeChild objOld
 Next
 objDoc.Save strFile
 Set objOld = Nothing
 End Function
End Class
%>

0 条评论:

发表评论

指向此帖子的链接:

创建链接

<< 主页