大家一起来讨论XML和Class的应用把:)~~参与就有分

itboy3000 2003-09-28 11:17:51
题目:使用XML存储模块的配置,然后构建一个Class来读入它,使用很简洁的语法来获得配置信息.

XML格式如下:
a.XML
===================================================
<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Layer1 ID="A" Key1="A1" Key2="A2"/>
<Layer1 ID="B" Key1="A1" Key2="A2">
<Layer2 ID="C" Key1="C1" Key2="C2"/>
<Layer2 ID="D" Key1="D1" Key2="D2"/>
</Layer1>
</Root>
===================================================

希望能用如下的语句访问数据:
===================================================
Set MyModule = New Module()
MyModule.Init("a.xml")
Response.Write MyModule.Layer1("A").Key1 '期望输出A1
Response.Write MyModule.Layer1("B").Layer2("C").Key2 '期望输出C2
...全文
45 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
cngis 2003-09-28
  • 打赏
  • 举报
回复
正在关注中
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
新的代码测试,可以实现如同
MyModule.Items("Pages").AllPages("a").Name 方式访问

不过还是没有达到如同 MyModule.Pages.a.Name的访问方式~
=======================================================================
<%
Class Page
Public ID,Name
End Class

Class Pages
Public Count
Public AllPages

Function Init()
Set AllPages = Server.CreateObject("Scripting.Dictionary")
End Function

Function AddPage(ID,Name)
Count = Count + 1
Set K = New Page
K.ID = ID
K.Name = Name
Call AllPages.Add(ID,K)
End Function

End Class

Class Module
Public Items
Function Init()
Set Items = Server.CreateObject("Scripting.Dictionary")

Set MyPages = New Pages
MyPages.Init()
Call MyPages.AddPage("a","aa")
Call MyPages.AddPage("b","bb")

Call Items.Add("Pages",MyPages)
Call Items.Add("Count",23)
End Function
End Class



Set MyModule = New Module
MyModule.Init()

Response.Write MyModule.Items("Pages").AllPages("a").Name
Response.Write MyModule.Items("Pages").AllPages("b").Name

%>
qlwqz 2003-09-28
  • 打赏
  • 举报
回复
up
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
呵呵,看来大家不是很感兴趣啊.那谁想要分就留个信,一会结贴
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
抛砖引玉.这个是我写的代码,使用字典对象,不过应该还有更好的方法,大家一起讨论啊

Event.XML
==============================================
<?xml version="1.0" encoding="GB2312" ?>
<Module ID="OA_Event" Name="公司大事记" Description="用于记录公司的重大事件,包含4个页面,3个操作和2组权限" DefaultPage="List" ModuleType="1">
<Pages>
<Page ID="List" Name="列表" Src="List.asp" Level="1" Description="列出所有公司大事的索引信息"/>
<Page ID="Addnew" Name="添加" Src="Addnew.asp" Level="1" Description="添加一条新公司大事(表单页)"/>
<Page ID="View" Name="查看" Src="View.asp" Level="2" Description="查看一条公司大事">
<Parameter IsMust="1" Record="ID" Method="Get">ID</Parameter>
</Page>
<Page ID="Edit" Name="编辑" Src="Edit.asp" Level="2" Description="修改一条公司大事(表单页)">
<Parameter IsMust="1" Record="ID" Method="Get">ID</Parameter>
</Page>
</Pages>
<Operations>
<Operation ID="Addnew" Name="添加" Method="Post" Src="Addnew.asp" Level="1" Description="执行添加一条新公司大事"/>
<Operation ID="Edit" Name="编辑" Method="Post" Src="Edit.asp" Level="2" Description="执行修改一条公司大事"/>
<Operation ID="Delete" Name="删除" Method="Get" Src="Delete.asp" Level="2" Description="执行删除一条或者多条公司大事">
<Parameter IsMust="1" Record="IDs">IDs</Parameter>
</Operation>
</Operations>
<Permissions>
<Permission ID="Viewer" Name="读者" Description="用户有查看公司大事记录的权限" Pages="List,View" />
<Permission ID="Author" Name="作者" Description="用户有添加新公司大事记录的权限" Pages="List,View,Addnew" Operations="Addnew"/>
</Permissions>
<Records>
<Record ID="ID" />
<Record ID="IDs" />
</Records>
</Module>
==========================================================

Module.asp
============================================================
<%Option explicit%>
<%
'-----------------------------------------------------------------
'---模块类---------------------------------------------------------
Class Module
Public Heads,Pages,Operations
Private ModuleURL
Private StrXMLURL,ObjXML
Private Node
Private ObjDic

'初始化一个模块,传入模块配置文件的URL
Public Function Init(ModuleURL)
Set ObjXML = Server.CreateObject("Microsoft.XMLDom")
ObjXML.Load(Server.MapPath(ModuleURL))
Set Heads = GetHeads()
Set Pages = GetPages()
End Function

'获取模块头信息
Private Function GetHeads()
Dim ObjDic,i,Node
Set ObjDic = Server.CreateObject("Scripting.Dictionary")
Set Node = ObjXML.SelectSingleNode("//Module")
For i = 1 To Node.attributes.length
Call ObjDic.Add(Node.attributes(i-1).BaseName,Node.attributes(i-1).Value)
Next
Set GetHeads = ObjDic
End Function

'获取模块页面信息
Private Function GetPages()
Dim ObjDic,i,j,Node,N
Dim ArrDic
Set ObjDic = Server.CreateObject("Scripting.Dictionary")
Set Node = ObjXML.SelectSingleNode("//Pages")
i = 0
For Each N In Node.childNodes
i = i + 1
Redim ArrDic(i)
Set ArrDic(i) = Server.CreateObject("Scripting.Dictionary")

For j = 1 To N.attributes.length
Call ArrDic(i).Add(N.attributes(j-1).BaseName,N.attributes(j-1).Value)
Next
Call ObjDic.Add(N.GetAttribute("ID"),ArrDic(i))
Next
Set GetPages = ObjDic
End Function
End Class
'-----------------------------------------------------------------

Dim ObjM
Set ObjM = New Module
ObjM.Init("Modules/XML/Event.xml")



%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<Title><%=ObjM.Heads("Name")%></Title>
</HEAD>
<BODY>

<P>

<%
Dim AllPages,PageIDs,PageMSG
Dim N
Set AllPages = ObjM.Pages
PageMSG = AllPages.Items

For Each N In PageMSG
Response.Write "ID:" & N("ID") & "<br>"
Response.Write "Name:" & N("Name") & "<br>"
Response.Write "<hr>"
Next

Response.Write AllPages("Edit")("Src")



%>
</P>
</BODY>
</HTML>
pp4u 2003-09-28
  • 打赏
  • 举报
回复
没人关系拉用asp实现太麻烦,不如用ASP.NET呢。
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
不实用吗大家都觉得?
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
好了,谢谢大家的提示和帮助,现在已经基本上完成了,代码如下:

<%Option explicit%>
<%
'********************************************************************
' 模块配置信息解析
' 作者: bitboy(itboy3000) (itboy3000@sina.com)
'********************************************************************
'---模块类---------------------------------------------------------
Class Module
Public Heads,Pages,Operations
Public ID,Name,Description,DefaultPage,ModuleType
Private ModuleURL
Private StrXMLURL,ObjXML
Private Node
Private ObjDic

'初始化一个模块,传入模块配置文件的URL
Public Function Init(ModuleURL)
Set ObjXML = Server.CreateObject("Microsoft.XMLDom")
ObjXML.Load(Server.MapPath(ModuleURL))
Set Heads = GetHeads() '将头信息传给对象Heads
Set Pages = GetPages() '将页面信息传给对象Pages
Set Operations = GetOperations() '将操作信息传给对象Operations
End Function

'获取模块头信息
Private Function GetHeads()
Dim ObjDic,i,Node
Set ObjDic = Server.CreateObject("Scripting.Dictionary")
Set Node = ObjXML.SelectSingleNode("//Module")
For i = 1 To Node.attributes.length
Call ObjDic.Add(Node.attributes(i-1).BaseName,Node.attributes(i-1).Value)
Next
ID = Node.GetAttribute("ID")
Name = Node.GetAttribute("Name")
DefaultPage = Node.GetAttribute("DefaultPage")
Description = Node.GetAttribute("Description")
ModuleType = Node.GetAttribute("ModuleType")
Set GetHeads = ObjDic
End Function

'获取页面信息
Private Function GetPages()
Dim ObjDic,i,j,Node,N,M,O
Dim EachItem,EachParameter,MyParameters
Set ObjDic = Server.CreateObject("Scripting.Dictionary")
Set Node = ObjXML.SelectSingleNode("//Pages")
'找出所有Pages下的Page标签总数
For Each N In Node.childNodes
Set EachItem = New Page
EachItem.ID = N.GetAttribute("ID")
EachItem.Name = N.GetAttribute("Name")
EachItem.Src = N.GetAttribute("Src")
EachItem.Level = N.GetAttribute("Level")
EachItem.Description = N.GetAttribute("Description")
EachItem.PerameterCount = 0
Set MyParameters = Server.CreateObject("Scripting.Dictionary")
'找出所有Pages/Page/Parameters/Parmeter标签
For Each M In N.childNodes
If M.NodeName = "Parameters" Then
For Each O In M.childNodes
If O.NodeName = "Parameter" Then
Set EachParameter = New Parameter
EachParameter.ID = O.GetAttribute("ID")
EachParameter.Name = O.GetAttribute("Name")
EachParameter.IsMust = O.GetAttribute("IsMust")
EachParameter.Record = O.GetAttribute("Record")
EachParameter.Method = O.GetAttribute("Method")
EachItem.PerameterCount = EachItem.PerameterCount + 1
Call MyParameters.Add(EachParameter.ID,EachParameter)
End If
Next
End If
Next
Set EachItem.Parameters = MyParameters
Call ObjDic.Add(N.GetAttribute("ID"),EachItem)
Next
Set GetPages = ObjDic
End Function

'获取操作信息
Private Function GetOperations()
Dim ObjDic,i,j,Node,N,M,O
Dim EachItem,EachParameter,MyParameters
Set ObjDic = Server.CreateObject("Scripting.Dictionary")
Set Node = ObjXML.SelectSingleNode("//Operations")
'找出所有Operations下的Operation标签总数
For Each N In Node.childNodes
Set EachItem = New Operation
EachItem.ID = N.GetAttribute("ID")
EachItem.Name = N.GetAttribute("Name")
EachItem.Src = N.GetAttribute("Src")
EachItem.Level = N.GetAttribute("Level")
EachItem.Description = N.GetAttribute("Description")
EachItem.PerameterCount = 0
Set MyParameters = Server.CreateObject("Scripting.Dictionary")
'找出所有Operations/Operation/Parameters/Parmeter标签
For Each M In N.childNodes
If M.NodeName = "Parameters" Then
For Each O In M.childNodes
If O.NodeName = "Parameter" Then
Set EachParameter = New Parameter
EachParameter.ID = O.GetAttribute("ID")
EachParameter.Name = O.GetAttribute("Name")
EachParameter.IsMust = O.GetAttribute("IsMust")
EachParameter.Record = O.GetAttribute("Record")
EachParameter.Method = O.GetAttribute("Method")
EachItem.PerameterCount = EachItem.PerameterCount + 1
Call MyParameters.Add(EachParameter.ID,EachParameter)
End If
Next
End If
Next
Set EachItem.Parameters = MyParameters
Call ObjDic.Add(N.GetAttribute("ID"),EachItem)
Next
Set GetOperations = ObjDic
End Function
End Class


'---Page类---------------------------------------------------------
Class Page
Public ID,Name,Src,Level,Description,Parameters,PerameterCount
End Class

'---Parameter类---------------------------------------------------------
Class Parameter
Public ID,Name,IsMust,Record,Method
End Class

'---Operation类---------------------------------------------------------
Class Operation
Public ID,Name,Src,Level,Description,Parameters,PerameterCount
End Class

'---Permission类---------------------------------------------------------
Class Permission
Public ID,Name,Src,Level,Description,Pages,Operations
End Class


'********************************************************************



%>



<%
'初始化
Dim ObjM
Set ObjM = New Module
ObjM.Init("Modules/XML/Event.xml")


'例子一:取得模块信息
Response.Write "模块ID:" & ObjM.ID & "<br>"
Response.Write "模块名称:" & ObjM.Name & "<br>"
Response.Write "模块描述:" & ObjM.Description & "<br>"
Response.Write "<hr>"


'例子二:历遍所有的Page
Dim MyPage
Set MyPage = New Page
For Each MyPage In ObjM.Pages.Items
Response.Write "ID:" & MyPage.ID & "<br>"
Response.Write "Name:" & MyPage.Name & "<br>"
Response.Write "Src:" & MyPage.Src & "<br>"
Response.Write "<br>"
Next
Response.Write "<hr>"


'例子三:取得给定页面的描述
Response.Write ObjM.Pages("Edit").Description
Response.Write "<hr>"

'例子四:取得给操作的所有参数
Dim MyParameter
Set MyParameter = New Parameter
For Each MyParameter In ObjM.Operations("Delete").Parameters.Items
Response.Write MyParameter.ID & ":" & MyParameter.Name & ":" & MyParameter.IsMust & "<br>"
Next
Response.Write "<hr>"
%>


Event.xml格式稍微改动过,具体如下
=============================================
<?xml version="1.0" encoding="GB2312" ?>
<Module ID="OA_Event" Name="公司大事记" Description="用于记录公司的重大事件,包含4个页面,3个操作和2组权限" DefaultPage="List" ModuleType="1">
<Pages>
<Page ID="List" Name="列表" Src="List.asp" Level="1" Description="列出所有公司大事的索引信息"/>
<Page ID="Addnew" Name="添加" Src="Addnew.asp" Level="1" Description="添加一条新公司大事(表单页)"/>
<Page ID="View" Name="查看" Src="View.asp" Level="2" Description="查看一条公司大事">
<Parameters>
<Parameter ID="ID" Name="公司记录ID" IsMust="1" Record="ID" />
</Parameters>
</Page>
<Page ID="Edit" Name="编辑" Src="Edit.asp" Level="2" Description="修改一条公司大事(表单页)">
<Parameters>
<Parameter ID="ID" Name="公司记录ID" IsMust="1" Record="ID" />
</Parameters>
</Page>
</Pages>
<Operations>
<Operation ID="Addnew" Name="添加" Method="Post" Src="Addnew.asp" Level="1" Description="执行添加一条新公司大事"/>
<Operation ID="Edit" Name="编辑" Method="Post" Src="Edit.asp" Level="2" Description="执行修改一条公司大事"/>
<Operation ID="Delete" Name="删除" Method="Get" Src="Delete.asp" Level="2" Description="执行删除一条或者多条公司大事">
<Parameters>
<Parameter ID="IDs" Name="公司记录ID集合,使用逗号分隔" IsMust="1" Record="IDs"/>
</Parameters>
</Operation>
</Operations>
<Permissions>
<Permission ID="Viewer" Name="读者" Description="用户有查看公司大事记录的权限" Pages="List,View" />
<Permission ID="Author" Name="作者" Description="用户有添加新公司大事记录的权限" Pages="List,View,Addnew" Operations="Addnew"/>
</Permissions>
<Records>
<Record ID="ID" Name="公司记录ID"/>
<Record ID="IDs" Name="公司记录ID集合,使用逗号分隔" />
</Records>
</Module>
Heqikun 2003-09-28
  • 打赏
  • 举报
回复
up
lions911 2003-09-28
  • 打赏
  • 举报
回复
多谢分享代码!很不错!接分!
lions911 2003-09-28
  • 打赏
  • 举报
回复
BITBOY的代码很好!这个正是我比较感兴趣的东西!谢谢!
angelheavens 2003-09-28
  • 打赏
  • 举报
回复
up
soqetsu 2003-09-28
  • 打赏
  • 举报
回复
使用的次数不多,正在考虑中
BainStudio 2003-09-28
  • 打赏
  • 举报
回复
UP.来晚了.
StarLee 2003-09-28
  • 打赏
  • 举报
回复
我来接分
mzcih 2003-09-28
  • 打赏
  • 举报
回复
给多少分我呢?
云帆 2003-09-28
  • 打赏
  • 举报
回复
up,我要向asp.net进发!!!!!
itboy3000 2003-09-28
  • 打赏
  • 举报
回复
呵呵,是个尝试性的应用

建立一个信息平台框架,平台提供标准的XML接口,各个子模块根据该XML接口配置信息,然后就可以正常运行在该信息平台框架上

信息平台框架可以管理模块的页面/权限分配/样式/,并可通过自己的工作流引擎组织各个模块之间的协同工作

有点意思把:)
xxrl 2003-09-28
  • 打赏
  • 举报
回复
楼主可以发代码到我的邮箱,我想研究一下楼主的代码
jjh_115@sina.com
我想要知道楼主的代码的应用所在
caodavid2003 2003-09-28
  • 打赏
  • 举报
回复
.
.
.
===================================================

希望能用如下的语句访问数据:
===================================================
Set MyModule = New Module()
MyModule.Init("a.xml")
Response.Write MyModule.Layer1("A").Key1 '期望输出A1
Response.Write MyModule.Layer1("B").Layer2("C").Key2 '期望输出C2
-------------------------------------------------------------------
你可以定义两个类 一个Module类,一个Layer1类
ID、Key1、Key2作为Layer1的公共数据成员(public)
Module要继承有关读取xml的类
Layer1作为Module的数据成员
Module要有解析、读取xml的方法

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧