Using Asp+xml to create personalized message book

Source: Internet
Author: User
Tags chr copy
Asp+|xml 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, have a basic understanding of XML and DOM, if you do not understand then you can basically hold the Asp+xml and DOM applications by studying this article, and can write out the Asp+xml programs that are more advanced 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 to store message information with XML data to achieve the function of storing data in the same database.

   third, the advantages of the XML message book

Perhaps you will be asked to use Asp+txt text can also be achieved without the database of the function of the message, good indeed can do, and there are many online such a message 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 when the speed is surprisingly fast, but as the message information increased TXT text, speed is surprisingly slow this is the weakness of asp+txt. Of course I can't say asp+xml must have been surprisingly fast, but it's a lot quicker than asp+txt, of course, with the increase in message information the XML text increases quickly, but it's a lot better than txt (this can be tested from the was test that readers can test themselves), Of course, asp+xml than the database, because the database for the query to do a special optimization, and XML is only plain text, in the ASP to create the image is to read all the XML data into memory, if the amount of data can imagine the speed will slow 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 going to mention here that when the data is more complex and random, it's more appropriate to use XML data, and that's when you plan to read and use the data on different operating systems. 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 must read a row of judgment, but also very multi-functional can not be, can only prepare a relatively simple message, and the XML data is different, using DOM can easily access each node, Instead of txt those annoying readline () witeline (), we can randomly add delete update a certain we are interested in the node, the use of ASP or JS or data island can easily do this, of course I am here to consider compatibility, with ASP to read XML data, Instead of using the data island to read the node data (since only IE5 versions support data island Technology) and ASP to actually do not exist these questions because the customer is getting HTML files.

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 should be changed. 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 (if you are interested in this you can certainly add one that does not affect the program's operation). 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-the meaning of the <Posttime>

4, User homepage set-righteousness is
5, the user's mailbox--defined as <email>

6, message content set-the meaning of the <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> Harsh child </username>
<fromwhere> Guangzhou, China </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. The is the root node , and so on, as child nodes. 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 the DOM to isolate the data we want.

<%
strSourceFile = Server.MapPath ("/") & "\list.xml"
'get the path to the XML file this 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 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" >
<TD colspan= "2" ><font color= "#FFFFFF" "Yi 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
'Gets the child node data (since the maximum number of child nodes from the number of nodes starting from 0 is reduced by 1)
Pagenum=allnodesnum\pagesize+1 'figure out the total number of pages
Pageno=request.querystring ("PageNo")
If pageno= "" Then 'If you are getting the page each time, navigate to each page to display the latest messages
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 RCAs bounds error
Starnodes=allnodesnum
End If
If endnodes<0 Then
Endnodes=0
End If
While Starnodes>=endnodes
'reading 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, apparently objrootsite the corresponding node of the image is
'The corresponding node of ObjRootsite.childNodes.item (Starnodes) is the node because it is not a node, so the item () is used to identify the current node data. With the descending of the Starnodes the node root moves up one by one to read node data.
'ObjRootsite.childNodes.item (starnodes). Childnodes.item (1). text corresponds to the node is specific to each node of the text value, here (1) The corresponding is the username username (because the child node of the node is what we really want )
'******************************************************************************

Text = replace (TEXT,CHR (), "<br>")
'Alternate Carriage return
Text = replace (TEXT,CHR (32), "")
'Alternate Space
%>
<TR bgcolor= "#F0F0F0" >
<TD width= "21%" height= "valign=" "Top" > name: <%=username%> <br>
From: <%=fromwhere%> <br>
</td>
<TD width= "79%" height= "valign=" "Top" | <a Href= "<%=homepage%> target=_blank title=" <%=username%> Home "> home </a>
| | <a Href= "mailto: <%=email%> title=" to <%=username%> letter "> box </a> | |<font color=" #CC6633 ">
Message time: <%=Posttime%> </font> |
<%=text%> </td>
</tr>
<TR bgcolor= "#FFFFFF" align= "right"
<TD colspan= "2" > </td>
</tr>
<%
Starnodes=starnodes-1
Wend
Set objxml=nothing
%>
<TR bgcolor= "#FFFFFF" align= "right"
<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, and join our generated XML node with the AppendChild () method.

<%
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 this 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 the node
If Objrootlist.haschildnodes Then
' To determine if has child nodes (because if is not a child node every time),
’ If you don't make a judgment, you'll get an error at the first shipment.
id = objrootlist.lastchild.firstchild.text+1
' This 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 , plus 1 (here we increment by 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 a line of nodes in an XML file and arrange them neatly
’ (Of course you can 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 the objXML2
Objrootlist.appendchild (Rootnewnode)
' inserting XML fragments into the List.xml
Objxml.save (strSourceFile)
' store lsit.xml files (because List.xml is only updated in memory because it is not stored)
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" >
<TD colspan= "2" ><font color= "#FFFFFF" > new message </font> </td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" name: </td>
<TD width= "81%" >
<input type= "text" name= "username"
* * </td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" from: </td>
<TD width= "81%" >
<input type= "text" name= "Fromwhere" value= "China"
</td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" homepage: </td>
<TD width= "81%" >
<input type= "text" name= "homepage" value= "http://"
</td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" >email: </td>
<TD width= "81%" >
<input type= "text" name= "email"
</td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" valign= "Top" "Content: </td>
<TD width= "81%" >
<textarea name= "text" cols= "a" rows= "ten" > </textarea>
</td>
</tr>
<TR bgcolor= "#EFEFEF" >
<TD width= "19%" align= "right" > </td>
<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)

(postnew.asp results)

Finally, if you copy my above program does not run, but it is copy or I comment error (but the home page to check strsourcefile = Server.MapPath ("/") & "\list.xml" Here is wrong, But I can tell you if the hint is missing to like so many is here to make a mistake, finally you can go to http://Page2000.xiloo.com to download the source program, also can give me a letter asp_boys@cmmail.com I will send you the source program as soon as possible. If you have any questions you can go to Http://www.4u2v.com/bbs because I'm there to answer some of the questions I know.

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.