<%
'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>
%>

2009/04/21 10:52 2009/04/21 10:52

Trackback Address :: https://youngsam.net/trackback/332