给初学者的,用xml实现内容的动态显示
这个动态不是用javascript动态显示,而是从即时数据库读取数据,并显示出来。
就象现在的csdn,msdn菜单一样,因为数据量很大,不可能在页面装载的时候把
所有的内容都成数据库中读出来,只能当用户请求某部分数据的时候才从数据库
中读取相应部分,因为这个读取的数据量并不大(只读取相关节点),所以显示速度
还是可以接受的,而且读取操作只进行一次,如果一个节点被请求过,以后就不需要
重新读取了。
事实上,网上已经有很多相关代码了,而且可以通过直接查看csdn代码的方式得到代码。但是我看了这些代码都很复杂,并不利于初学者学习,于是我自己写了一个,希望对你们有帮助。
为了说明问题,我的这个代码很简单,很容易看懂的,主要演示了 xml数据岛的应用
动态请求数据库的技巧。
功能是:
1。用数据岛显示文章标题(只是标题,内容并没有装载)
2。当用户点某一文章标题的时候才从数据库读取该文章的内容,并显示出来
有两个文件。 list.htm显示文章标题列表。 getxml.asp 根据请求返回一个xml文件
list.htm
<html>
<script>
function showHide(el){ //点击标题的时候,显示或隐藏该文章内容
var el=el.nextSibling;
if (el.style.display=="none"){
el.style.display="block";
//如果文章没有装载过,就去数据库读取
if (!el.getAttribute("loadok")) loadContent(el)
}else{
el.style.display="none";
}
}
function loadContent(el){ //从数据库读取文章内容,并显示出来
var xmldoc = new ActiveXObject("MSXML.DOMDocument");
xmldoc.load("getxml.asp?title="+el.previousSibling.innerHTML);
xmldoc.onreadystatechange =function(){
if (xmldoc.readyState!=4) return;
try{ //显示内容
el.innerHTML=xmldoc.documentElement.firstChild.firstChild.nodeValue;
}catch(x){
el.innerHTML="装载数据失败";
}finally{//设置读取标记,说明该节点的内容已经从数据库读回来了
el.setAttribute("loadok","ok");
xmldoc=null;
}
}
}
</script>
<body onselectstart="return false">
<xml id="dso" src="getxml.asp?type=title"></xml>
<table datasrc="#dso"><tr><td>
<div datafld="title" style="cursor:hand;" onclick="showHide(this)">正在加载....</div>
<div style="padding-left:30px;display:none;cursor:default">正在加载....</div>
</td></tr></table>
</body>
</html>
2。getxml.asp
<%
response.ContentType ="text/xml"
response.expires = 0
response.expiresabsolute = Now() - 1
response.addHeader "pragma","no-cache"
response.addHeader "cache-control","private"
Response.CacheControl = "no-cache"
set conn=server.CreateObject("adodb.connection")
conn.open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=SMILE"
set rs= server.CreateObject("adodb.recordset")
Response.Write "<?xml version='1.0' encoding='gb2312'?>" & vbcrlf
Response.Write "<data>" & vbcrlf
'这里,我设置了一个空循环,用来延间,这样好看到效果
for i=1 to 1000000
next
if trim(lcase(Request.QueryString("type")))="title" then
'读所有标题
sql="select [title] from [test]"
rs.Open sql,conn,1,1
while not rs.EOF
Response.Write "<node><title><![CDATA["&trim(rs(0))&"]]></title></node>" & vbcrlf
rs.MoveNext
wend
else '读取指定的文章内容
sql="select [content] from [test] where [title]='" & replace(trim(Request.QueryString("title")),"'","''") & "'"
rs.Open sql,conn,1,1
if not rs.EOF then Response.Write "<content><![CDATA[" & trim(rs(0)) & "]]></content>" & vbcrlf
end if
Response.Write "</data>" & vbcrlf
rs.Close
set rs=nothing
set conn=nothing
%>
最后,数据库表
CREATE TABLE [test]( [title] varchar(40) primary key,
[content] varchar (4000)
)
insert into test values('title1','content1')
insert into test values('title2','content2')
insert into test values('title3','content3')
insert into test values('title4','content4')
insert into test values('title5','content5')
insert into test values('title6','content6')
insert into test values('title7','content7')
insert into test values('title8','content8')
insert into test values('title9','content9')
希望能对你们有帮助,其实CSDN的那种菜单并没有你们想象的那么神秘,
如果理解了这个两层的,那以后做三层,n层动态菜单都不在话下了。