Use ASP + XML to create a message book

Source: Internet
Author: User

Use ASP + XML to create a message book

I. Reader Guide
Reader guide helps you understand the synopsis of this article. So that you may not understand this part after reading it.ArticleIt is not suitable for you and causes visual pollution to you.

If you are using ASP + XML to write someProgramOr you are learning XML.

Read the knowledge required in this article, have a basic understanding of ASP, and have a basic understanding of XML and Dom. If you do not understand it, you can
Support ASP + XML and Dom applications, and write more advanced ASP + XML programs based on this article.

Ii. Basic Ideas

The idea of this article is to use ASP and Dom to read and store XML data, and use XML data to store message information, to achieve the same purpose of storing data in databases.
Yes.

Iii. Advantages of XML message book

You may ask whether ASP + TXT text can be used to achieve the function of not having to leave a message on the database. It is good and can be done, and there are many such functions on the Internet.
This is free to download, but here I want to talk about the advantages of ASP + XML.

1. XML + ASP is faster than ASP + TXT. You may also find that when there are few TXT files, the speed is surprisingly fast, but as the message information increases, the TXT text
The speed is surprisingly slow, which is the weakness of ASP + TXT. Of course, I cannot say that ASP + XML must be surprisingly fast,
P + txt is much faster. Of course, the XML text increases rapidly with the increase of message information, but it is much better than TXT.
(This can be proved by was testing.) Of course, ASP + XML is not as good as the database, because the database has made special Optimizations in queries.
While XML is only plain text, it seems like you want to read all the XML data into the memory when you create a pair in ASP. If the data volume is large, it can be imagined that the speed will slow down.
Then you may ask, when will I use relational databases to store data and XML to store data? By the way, when we compare data
It is more appropriate to use XML data for complex random rates, and when you plan to read and use the data on different operating systems, it is time for XML to show its strength.
. If you use common data, do not use XML files to store data when you do not have a critical moment or do not support database space.
2. XML data is easy to read, and TXT text is difficult to operate. We have to read and judge one row at a time. In addition, many functions cannot be implemented and can only be edited.
A simple message book, while XML data is different. Using Dom, you can easily access every node, instead of the annoying Readline () of TXT ()
Witeline (), we can add or delete a node we are interested in at will, and use ASP, JS, or data island to easily implement this
Point, of course, I will consider compatibility here. I use ASP to read XML data, instead of data island to read node data (because only ie5 or a later version is used ).
Support data island technology) but ASP does not actually exist because the customer obtains HTML files.

3. XML data boast a systematic approach. As long as we store the data as XML, the data can be identified by the language or system.
What changes should be made. TXT obviously does not have these performances. For example, our message on the Internet can be directly converted to the WAP format and displayed on the mobile phone.

4. Create an XML file (list. XML) for storing message information)
We do not intend to use DTD here, because we have compiled and tested the XML data, so we do not need DTD for verification (if you
Of course, you can add one that does not affect the running of the program ). Now let's take a look at the basic elements of creating a message book.

0. Message ID -- defined as <ID>

1. Username -- defined as <username>

2. From there -- defined as <fromwhere>

3. Set the message time to <posttime>

4. Set the user homepage to

5. user mailbox-defined as <email>

6. Set the message content to <text>

Of course, the readers who are not required can name and add or remove related labels by themselves, and combine them to get the list. xml file.

<? XML version = "1.0" encoding = "gb2312"?>
<Site>
<Newlist>
<List>
<ID> 1 </ID>
<Username> example </username>
<Fromwhere> Guangzhou, China </fromwhere>
<Posttime> 01-4-23 18:26:56 </posttime>
<Homepage> http://page2000.xiloo.com <Email> lucaihui@cmmail.com </Email>
<Text> This is my first XML file </text>
</List>
</Newlist>
</Site>

Note: encoding = "gb2312" must be added; otherwise, an error is returned because XML does not support Chinese characters by default. <Site> It is the root node <newlist>
<Ist>. I will not talk much about the meaning of this file. You should also understand it. The next step is how to display it.

5. Create an ASP file (index. asp) that displays XML data)
The function of this file is to read and display XML data. First, create an XML Object and read the XML into the memory, and use the Dom to separate
Required data.

<%
Strsourcefile = server. mappath ("/") & "\ list. xml"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. freethreadedxmldom ")
'Create an XML object with a free thread
Objxml. Load (strsourcefile) 'reads XML files into memory
Set objrootsite = objxml.doc umentelement. selectsinglenode ("newlist ")
'Select A newlist node.
%>
<HTML>
<Head>
<Title> message book </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style type = "text/CSS">
<! --
TD {font-family: ""; font-size: 9pt; text-Decoration: None}
A {font-family: ""; font-size: 9pt; color: # 0066cc; text-Decoration: None}
A: hover {color: # ff6600; text-Decoration: underline}
-->
</Style>
</Head> <body bgcolor = "# 0099cc" text = "#000000">
<Table width = "80%" border = "0" cellspacing = "1" cellpadding = "4" align = "center" bgcolor = "# cccccc">
<Tr bgcolor = "#000000" type = "codeph" text = "/codeph">
<TD colspan = "2"> <font color = "# ffffff"> comment <a href = "postnew. asp"> <font color = # ffffff> write a new message
</Font> </a> </font> </TD>
</Tr>
<%
Pagesize = 10' suppose 10 messages are displayed on each page
Allnodesnum = objrootsite. childnodes. Length-1
'Obtain the subnode data (because the maximum number of subnodes starting from 0 must be reduced by 1)
Pagenum = allnodesnum \ pagesize + 1 calculate the total number of pages
Pageno = request. querystring ("pageno ")
If pageno = "" then', if each page is obtained, the latest message is displayed on each page.
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', determine whether the number of start nodes exceeds the total number of nodes.
Endnodes = endnodes-(starnodes-allnodesnum)
'If the value exceeds, the End Node minus the difference (starnodes-allnodesnum). Otherwise, the subscript will be out of bounds.
Starnodes = allnodesnum
End if
If endnodes <0 then
Endnodes = 0
End if
While starnodes> = endnodes
'Read node data from the end node to the superstart Node
Username = objrootsite. childnodes. Item (starnodes). childnodes. Item (1). Text
'Get Username
Fromwhere = objrootsite. childnodes. Item (starnodes). childnodes. Item (2). Text
'Get the user 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
'The received Response Message Content

'*************************************** **************************************
'Here we use the xml dom to read data. Obviously, the node corresponding to the object of the objrootsite is <newlist>
The node corresponding to 'objrootsite. childnodes. Item (starnodes) is the <list> node that uses item () because no <list> node exists ()
To identify the data of the current node. with the decreasing of starnodes, the <list> node reads the <list> node data one by one at the root of the node.
'Objrootsite. childnodes. Item (starnodes). childnodes. Item (1). the node corresponding to text is the text value of each node. Here
(1) username (because the sub-nodes of the <list> node are what we really want)
'*************************************** ***************************************

TEXT = Replace (text, CHR (13), "<br> ")
'Replacement carriage return
TEXT = Replace (text, CHR (32 ),"")
'Replacement space
%>
<Tr bgcolor = "# f0f0f0">
<TD width = "21%" Height = "94" valign = "TOP"> last name: <% = username %> <br>
From: <% = fromwhere %> <br>
</TD>
<TD width = "79%" Height = "94" valign = "TOP"> | <a href = "<% = homepage %>" target = _ blank Title = "<% = username %
> Home Page "> Home page </a>
| <A href = "mailto: <% = Email %> "Title =" write to <% = username %> "> mailbox </a> | <font color =" # cc6633 ">
Message time: <% = posttime %> </font> |
<HR>
<% = 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 pages <% = pagenum %>
<%
If CINT (pageno) <> pagenum then' page
Response. Write "<a href ="/index. asp? Pageno = "& (pageno + 1) &" '> previous page </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>
</Html>

6. Create a new message writing program (postnew. asp)
The function of this file is to write a new XML node. First, create an XML Object and read the XML into the memory. The appendchild () method is used.
Add the generated XML node.

<%
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"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. xmldom ")
'Create an XML Object
Objxml. Load (strsourcefile)
'Read XML files into memory
Set objrootlist = objxml.doc umentelement. selectsinglenode ("newlist ")
'Select the <newlist> node
If objrootlist. haschildnodes then
'Determine whether <newlist> has subnodes (because if each <newlist> operation has no subnodes,
'If no judgment is added, an error will be reported when the first operation is successful.
Id = objrootlist. lastchild. firstchild. Text + 1
'Obtain the ID number of the child node to be inserted. the ID number is the first child node (firstchild) of the last child node (lastchild) of <newlist>)
ID number plus 1 (Here we increase progressively according to the ID number of the relational database)
Else
'If there is no word subnode, it is the first time the Message ID is set to 1
Id = 1
End if
Brstr = CHR (13) & CHR (10) & CHR (9)
'To insert nodes in the XML file with line breaks and spaces arranged neatly
'(Of course you don't need to use it to make XML data look good)
Xmlnode = brstr & "<list>" & brstr &_
"<ID>" & ID & "</ID>" & brstr &_
"<Username>" & username & "</username>" & brstr &_
"<Fromwhere>" & fromwhere & "</fromwhere>" & brstr &_
"<Posttime>" & posttime & "</posttime>" & brstr &_
"<Homepage>" & homepage & ""<Email>" & Email & "</Email>" & brstr &_
"<Text>" & text & "</text>" & brstr &_
"</List>" & CHR (13)
'Create an XML Segment Based on the obtained data
Set objxml2 = server. Createobject ("Microsoft. xmldom ")
'Create a New XML Object
Objxml2.loadxml (xmlnode)
'Read XML snippets into memory
Set rootnewnode1_objxml2.doc umentelement
'Get the root node of objxml2
Objrootlist. appendchild (rootnewnode)
'Insert XML fragments into list. xml
Objxml. Save (strsourcefile)
'Store the lsit. xml file (because list. XML is not stored and only updated in memory)
Set objxml = nothing
Set objxml2 = nothing
Response. Write "thank you for your message"
Response. End

End if
%>
<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style type = "text/CSS">
<! --
TD {font-size: 9pt}
-->
</Style>
</Head>
<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" type = "codeph" text = "/codeph">
<TD colspan = "2"> <font color = "# ffffff"> new message </font> </TD>
</Tr>
<Tr bgcolor = "# efefef">
<TD width = "19%" align = "right"> last name: </TD>
& Lt; TD width = "81%" & gt;
<Input type = "text" name = "username">
** </TD>
</Tr>
<Tr bgcolor = "# efefef">
<TD width = "19%" align = "right"> from: </TD>
& Lt; TD width = "81%" & gt;
<Input type = "text" name = "fromwhere" value = "China">
</TD>
</Tr>
<Tr bgcolor = "# efefef">
<TD width = "19%" align = "right"> main page: </TD>
& Lt; TD width = "81%" & gt;
<Input type = "text" name = "Homepage" value = "http: //">
</TD>
</Tr>
<Tr bgcolor = "# efefef">
<TD width = "19%" align = "right"> Email: </TD>
& Lt; TD width = "81%" & gt;
<Input type = "text" name = "email">
</TD>
</Tr>
<Tr bgcolor = "# efefef">
<TD width = "19%" align = "right" valign = "TOP"> content: </TD>
& Lt; TD width = "81%" & gt;
<Textarea name = "text" Cols = "60" rows = "10"> </textarea>
</TD>
</Tr>
<Tr bgcolor = "# efefef">
& Lt; TD width = "19%" align = "right" & gt; & lt;/TD & gt;
& Lt; TD width = "81%" & gt;
<Input type = "Submit" name = "Submit" value = "Submit">
<Input type = "reset" name = "submit2" value = "Refill">
</TD>
</Tr>
</Form>
</Table>
</Body>
</Html>

VII. Summary

Here we will build a simple XML message book with paging functions. After reading the simple text, I will only use one of the functions here, the purpose is to guide you through the establishment of more complex and better ASP + XML programs, such as the above message book plus the reply function, management function, and click counting data function, you can even change it to a BBS. The running result of the message book is as follows:

(The running result of list. XML in ie5)

(Message book index. asp running 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.