Use Asp+xml to create a message this _ Application skills

Source: Internet
Author: User
Tags chr

Use Asp+xml to create a message book


First, the reader guide
The Reader guide helps you master the outline of this article. Lest you read most of it to understand this article is not suitable for you, causing you visual pollution.

If you're writing a program with Asp+xml, or you're learning XML, it's worth seeing.

Read the knowledge required in this article, have a basic understanding of the ASP, a basic understanding of XML and DOM, if you do not understand then you by learning this article can basically
Holds the Asp+xml and DOM applications, and can write from the more advanced Asp+xml program according to Ben Wen van.

Ii. Basic Ideas

The idea of this paper is to use ASP and DOM to read and store XML data, and use XML data to store message information to achieve the work of storing data in the same database.
Yes.

Third, the advantages of the XML message book

Perhaps you will ask to use asp+txt text can also achieve the function that does not use the database to actually leave a message, good indeed can do, and the net also has a lot of such stay
This free download, but here I want to talk about the advantages of asp+xml.

1, xml+asp faster than asp+txt speed. You may also find that when the TXT file is very rare, the speed is surprisingly fast, but with the addition of message txt text
Getting bigger, but the speed is surprisingly slow this is the weakness of asp+txt. Of course I can't say asp+xml must be surprisingly fast, but rather than as
P+txt is a lot faster, of course, with the increase in message information XML text increased speed also dropped quickly, but compared to TXT is still a lot better
(This can be shown from the was test that readers can test themselves), of course, asp+xml is not as good as the database, because the database for the query made a special excellent
, and XML is just plain text, in the ASP set up to be like to read all the XML data into memory, if the amount of data can be imagined slowed down.
Then you might ask me, when do I use relational databases to store data, and when do I store data in XML? I'm also here to mention that when data is compared
It's more appropriate to use XML data for complex random rates, and that's when you're going to read the data on different operating systems.
Out. Do not use XML files to store data if you are not in a critical moment or do not support the database's space.
2, easy to read the XML data, txt text is more difficult to operate, we have to read a line of judgment, but also very multi-functional can not be, can only be edited
Simple message book, and the XML data is different, using DOM can easily access each node, rather than txt those annoying readline ()
Witeline (), we can add at will delete update a certain we are interested in the node, using ASP or JS or data island can easily actually on this
Point, of course I'm here. Consider compatibility, use ASP to read XML data, and do not use data island to read node data (because only IE5 above version
Support Data island Technology and use ASP to actually do not exist these ask because the customer gets the HTML file.

3, the XML data Kua operating system, as long as we store this data as XML so that the data can be He Ki it language or system recognized, without doing
What changes should be made. TXT obviously does not have these performance. For example, our message on the Internet can be directly converted into a WAP format on the phone to display.

IV. create an XML file to store message information (list.xml)
We do not intend to use DTDs here, because we are the XML data that we have compiled and tested, so we do not need a DTD to validate it (if you
This interest of course can add a this does not affect the operation of the program. Now let's take a look at the basics of creating a message book.

0, Message ID number--defined as <id>

1, user name US--defined as <username>

2, from there--defined as <fromwhere>

3, message time Set-righteousness for <Posttime>

4, User homepage Set--Righteousness for

5, the user's mailbox--defined as <email>

6, message content set-righteousness for <text>

Of course, the above is not necessary to the reader can name and add and subtract related tags, put them together to get the List.xml file

<?xml version= "1.0" encoding= "gb2312"?>
<site>
<NewList>
<list>
<id>1</id>
<username> The harsh child </username>
<fromwhere> China Guangzhou </fromwhere>
<posttime>01-4-23 18:26:56</posttime>
<email>lucaihui@cmmail.com</email>
<text> This is my first XML file </text>
</list>
</NewList>
</site>


Note: You must add encoding= "gb2312" otherwise the error will be invalid because the XML does not support Chinese by default. <site> is the root node <NewList>
<ist> as a child node. I don't have much to say about the document, so we should see it. The next step is how we can show it.

V. Create ASP files to display XML data (index.asp)
The function of this file is to read and display the XML data, first create an XML image and then read the XML into memory, and use DOM to isolate the
The data you want.

<%
strSourceFile = Server.MapPath ("/") & "\list.xml"
' Get the path to the XML file, which is different depending on your virtual directory.
Set objxml = Server.CreateObject ("Microsoft.FreeThreadedXMLDOM")
' Create an XML pair as a free thread
Objxml.load (strsourcefile) ' reads the XML file into memory
Set objrootsite = ObjXML.documentElement.selectSingleNode ("NewList")
' Select the NewList node
%>
<title> Message Book </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<style type= "Text/css" >
<!--
TD {Font-family: "Song body"; font-size:9pt; Text-decoration:none}
A {font-family: "Song body"; Font-size:9pt color: #0066CC; Text-decoration:none}
a:hover {color: #FF6600; Text-decoration:underline}
-->
</style>
<table width= "80%" border= "0" cellspacing= "1" cellpadding= "4" align= "center" bgcolor= "#CCCCCC" >
<tr bgcolor= "#000000" >
&LT;TD colspan= "2" ><font color= "#FFFFFF" > Message pu <a href= "postnew.asp" ><font color= #FFFFFF > write new Message
</font></a></font></td>
</tr>
<%
PageSize = 10 ' Suppose each page shows 10 messages
Allnodesnum =objrootsite.childnodes.length-1
' Get the child node data (since the maximum number of child nodes from the number of nodes is 0 minus 1)
Pagenum=allnodesnum\pagesize+1 ' Calculate the total number of pages
Pageno=request.querystring ("PageNo")
If pageno= "then" if it is every time you get the page, navigate to each page to display the latest message
Pageno=pagenum
End If
Starnodes=pageno*pagesize-1 ' Get Start node
Endnodes= (PageNo-1) *pagesize ' Get End node
If Endnodes<0 Then
Endnodes=0
End If
If Starnodes>allnodesnum then ' determines whether the number of starting nodes exceeds the total number of nodes
endnodes=endnodes-(Starnodes-allnodesnum)
' If the difference between the end node minus (Starnodes-allnodesnum) is exceeded, the next rcas is out of bounds error
Starnodes=allnodesnum
End If
If Endnodes<0 Then
Endnodes=0
End If
While Starnodes>=endnodes
' Read node data from end node to super start node
Username = ObjRootsite.childNodes.item (starnodes). Childnodes.item (1). Text
' Get user name
Fromwhere = ObjRootsite.childNodes.item (starnodes). Childnodes.item (2). Text
' Get users from there
Posttime = ObjRootsite.childNodes.item (starnodes). Childnodes.item (3). Text
' Get message time
Homepage = ObjRootsite.childNodes.item (starnodes). Childnodes.item (4). Text
' Get user homepage
email = objRootsite.childNodes.item (starnodes). Childnodes.item (5). Text
' Get user Eamil
Text = ObjRootsite.childNodes.item (starnodes). Childnodes.item (6). Text
' Received yesterday's message content

'*****************************************************************************
' This makes the DOM of XML read the data, obviously objrootsite the corresponding node for the <NewList>
' ObjRootsite.childNodes.item (starnodes) corresponds to the node is <list> node because not to a <list> node so use the item ()
To identify the current node data, along with the descending of the Starnodes <list> node to move up one by one to read <list> node data.
' ObjRootsite.childNodes.item (starnodes). Childnodes.item (1). text corresponds to the node as a concrete value for each node, here
(1) The corresponding is the username user name (because the <list> node's child node is what we really want)
'******************************************************************************

Text = replace (TEXT,CHR (), <br>)
' Instead of carriage return
Text = replace (TEXT,CHR (32), "")
' Alternate spaces
%>
<tr bgcolor= "#F0F0F0" >
Width= "21%" height= "&LT;TD" valign= "Top" > Name:<%=username%><br>
From:<%=fromwhere%><br>
</td>
&LT;TD width= "79%" height= "valign=" "Top" > | <a href= "<%=homepage%>" Target=_blank title= "<%=username%
> Home "> Home </a>
| | <a href= "mailto:<%=email%>" title= "Give <%=username%> write" > Mailbox </a> | |<font color= "#CC6633" >
Message Time:<%=posttime%> </font>|
<%=text%> </td>
</tr>
<tr bgcolor= "#FFFFFF" align= "right" >
&LT;TD colspan= "2" > </td>
</tr>
<%
Starnodes=starnodes-1
Wend
Set objxml=nothing
%>
<tr bgcolor= "#FFFFFF" align= "right" >
&LT;TD colspan= "2" > Total <<%=PageNum%>> page
<%
If CInt (pageno) <>pagenum Then ' paging
Response.Write "<a href="/index.asp? Pageno= "& (pageno+1) &" ' > Prev </a> "
End If
If CInt (pageno) <>1 Then
Response.Write "<a href="/index.asp? Pageno= "& (PageNo-1) &" ' > Next page </a> "
End If
%>
</td>
</tr>
</table>
</body>

Vi. establishment of a new message program (postnew.asp)
The function of this file is to write a new XML node, first create an XML image and then read the XML into memory, using the AppendChild () method
Join the XML node we generated.

<%
Username=request.form ("username")
If username<> "" Then
Fromwhere =request.form ("Fromwhere")
Homepage =request.form ("homepage")
Email =request.form ("email")
Text =request.form ("text")
Text =replace (text, "<", "<")
Posttime =now ()
strSourceFile = Server.MapPath ("/") & "\list.xml"
' Get the path to the XML file, which is different depending on your virtual directory.
Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
' Create an XML pair like
Objxml.load (strSourceFile)
' Read the XML file into memory
Set objrootlist = ObjXML.documentElement.selectSingleNode ("NewList")
' Select <NewList> node
If Objrootlist.haschildnodes Then
' Determine whether <NewList> has child nodes (because if each <NewList> is not a child node,
' If you don't make a judgment, you'll get an error at the first time.
id = objrootlist.lastchild.firstchild.text+1
' Here gets the ID number of the child node that will be inserted, with the ID number of the first child node (firstchild) of the Last child node (lastchild) of <NewList>
ID number plus 1 (here we increment according to the ID number of the relational database)
Else
' If no word child node is the first time message ID number set to 1
Id=1
End If
BRSTR=CHR (&AMP;CHR) &AMP;CHR (9)
' To insert the nodes in the XML file to line up lines of space
' (Of course you can not do this just for the sake of XML data.)
xmlnode=brstr& "<list>" &brstr & _
"<id>" &id& "</id>" &brstr & _
"<username>" &username& "</username>" &brstr & _
"<fromwhere>" &fromwhere& "</fromwhere>" &brstr & _
"<Posttime>" &Posttime& "</Posttime>" &brstr & _
""<email>" &email& "</email>" &brstr & _
"<text>" &text& "</text>" &brstr & _
"</list>" &AMP;CHR (13)
' Create an XML fragment based on the resulting data
Set Objxml2=server.createobject ("Microsoft.XMLDOM")
' Create a new XML pair like
Objxml2.loadxml (XMLnode)
' Read the XML fragment into memory
Set Rootnewnode=objxml2.documentelement
' Get the root node of objXML2
Objrootlist.appendchild (Rootnewnode)
' Insert the XML fragment into the List.xml
Objxml.save (strSourceFile)
' Store Lsit.xml file (because no storage list.xml is updated in memory only)
Set objxml=nothing
Set objxml2=nothing
Response.Write "Thank you for your message."
Response.End

End If
%>
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<style type= "Text/css" >
<!--
td {FONT-SIZE:9PT}
-->
</style>
<body bgcolor= "#0099CC" text= "#000000" >
<table width= "80%" border= "0" cellspacing= "1" cellpadding= "4" align= "center" bgcolor= "#FFFFFF" >
<form action= "postnew.asp" method= "Post" Name= "Form1" >
<tr bgcolor= "#000000" >
&LT;TD colspan= "2" ><font color= "#FFFFFF" > New Message </font></td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" > name:</td>
&LT;TD width= "81%" >
<input type= "text" name= "username" >
* * </td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" > from:</td>
&LT;TD width= "81%" >
<input type= "text" name= "Fromwhere" value= "China" >
</td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" > home page:</td>
&LT;TD width= "81%" >
<input type= "text" name= "homepage" value= "http://" >
</td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" >Email:</td>
&LT;TD width= "81%" >
<input type= "text" name= "email" >
</td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" valign= "top" > Content:</td>
&LT;TD width= "81%" >
<textarea name= "text" cols= "rows=" ></textarea>
</td>
</tr>
<tr bgcolor= "#EFEFEF" >
&LT;TD width= "19%" align= "right" > </td>
&LT;TD width= "81%" >
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "refill" >
</td>
</tr>
</form>
</table>
</body>

Vii. Summary

Here we'll build a simple and paging XML message book, Ah, it is easy to read, in fact, I only have a starting point of the function, the purpose is to guide you to build more complex and better asp+xml procedures, such as: The above message this plus the reply function, management function, click the data function, etc. Even you can change it into a BBS and so on. Last message the results of this operation are as follows:


(The results of the operation List.xml under IE5)

(Message this index.asp run result)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.