[ASP] read/write configuration class (xml version)

|
<%
'Option Explicit

Class configs

Private mClassName
Private mConfigRootPath
Private mItems
Private mXmlRootNodeName
Private mConfigName

Private Sub Class_Initialize()
 mClassName = "configs class"
 mXmlRootNodeName = "configs"
 mConfigRootPath = server.mappath("./")
End Sub

Public Sub Load(configname)
 Dim xdom
 Dim i
 
 mConfigName = configname
 Set mItems = Server.CreateObject("scripting.dictionary")
 Set xdom = server.CreateObject("microsoft.xmldom")
 xdom.load mconfigrootpath & "\" & configname & ".xml"
 If xdom.parseerror.errorcode <> 0 Then
  Err.raise vbObjectError, mClassName, xdom.parseerror.reason
 Else
  if xdom.documentelement.nodename = mXmlRootNodeName Then
  For i=0 To xdom.documentelement.childnodes.length-1
  mItems.add xdom.documentelement.childnodes(i).nodename, xdom.documentelement.childnodes(i).text
  Next
  Else
  Err.raise vbObjectError, mClassName, "Invalid Configuration File"
  End If
 End If
 Set xdom = nothing
End Sub

Public Sub Save()
 Dim xdom
 Dim i

 Set xdom = server.CreateObject("microsoft.xmldom")
 xdom.load mconfigrootpath & "\" & mConfigName & ".xml"
 If xdom.parseerror.errorcode <> 0 Then
  Err.raise vbObjectError, mClassName, xdom.parseerror.reason
 Else
  if xdom.documentelement.nodename = mXmlRootNodeName Then
  For i= xdom.documentelement.childnodes.length-1 To 0 Step -1
  xdom.documentelement.removechild(xdom.documentelement.childnodes(i))
  Next
  Dim keys, items, newelement
  keys = mItems.keys()
  items = mItems.items()
  For i=0 To mItems.count-1
  Set newelement = xdom.createElement(keys(i))
  newelement.text = items(i)
  xdom.documentelement.appendchild newelement
  Set newelement = Nothing
  Next
  xdom.save mconfigrootpath & "\" & mConfigName & ".xml"
  Else
  Err.raise vbObjectError, mClassName, "Invalid Configuration File"
  End If
 End If
 Set xdom = nothing
 Set mItems = Nothing
End Sub

Public Sub Add(key, value)
 mItems.Add key, value
End Sub

Public Function Item(key)
 Item = mItems.Item(key)
End Function

End Class

'example.asp
'
'Dim a
'Set a = new configs
'a.load "example"
'response.write a.item("test1")
'a.add "test4", "33>3"
'a.save
'Set a = nothing

'example.xml
'
'<configs><test1>1111</test1><test2>2222</test2><test3>3333</test3></configs>
%>


출처 : http://hanhead.tistory.com/trackback/68

'프로그래밍 > asp, php' 카테고리의 다른 글

[ASP] Err 객체  (0) 2010.05.27
ON ERROR RESUME NEXT  (0) 2009.09.22
[ASP] asp로 xml 파일 저장  (0) 2009.08.12
[asp] 기본 함수  (0) 2009.08.01
Request.ServerVariables  (0) 2009.07.29
And