XML general application of ASP read and write documents

Source: Internet
Author: User
Tags contains reset trim
XML a simple press release system to speak of:

First, we create an XML document named Nndb.xml:

---------------------------------------Nndb.xml---------------------------------------
<?xml version= "1.0" encoding= "GB2312"?>
<data><news></news></data>

---------------------------------------Nndb.xml---------------------------------------

Then, do a published page, this release page with a list of news display, he named Post.asp Bar!

---------------------------------------post.asp---------------------------------------
<style>
#news_list {width:430px; float:left; border:1px solid #CFCFCF;}
#tt {width:300px; float:left; border-top:2px solid #CFCFCF;}
#pt {width:129px; float:right; border-top:2px solid #CFCFCF;}
#ct {width:430px; float:left; border-top:1px solid #CFCFCF;}
#news_list Li span {font-size:12px; letter-spacing:1px; font-family:tahoma;}
#c1 {width:40px float:left; padding:5px 0px 5px 0px; Background-color: #EFEFEF; font-weight:bold; color: #666; text- Align:center; border:1px solid #FFF; }
#c2 {float:left padding:5px; font-size:12px; color: #333; text-align:center; letter-spacing:1px; border:1px Solid #FFF; }
#c3 {width:380px float:right; margin:0px 5px 0px 5px; padding:3px 0px 3px 0px text-align:right; color: #CCC; Borde r-top:1px dotted #CFCFCF; }
</style>
<%
Set xmlobj = Server.CreateObject ("Microsoft.XMLDOM")
Xmlobj.async = False
Xmlobj.load Server.MapPath ("Nndb.xml")
Set news_root = XMLOBJ.documentElement.selectSingleNode ("News")
News_count = News_root.childNodes.length
    
Html_code = "<div id=" "News_list" ">"
For i = 0 to News_count-1
Set temp_news = News_root.childNodes.item (i). childnodes
Html_code = Html_code & "<li id=" "tt" "><span id=" "C1" "> Title </span><span id=" "C2" ">" & Temp_news.item (3). Text & "</span></li><li id=" "PT" "><span id=" "C1" "> Author </span> <span id= "" C2 "" > "& Temp_news.item (4). Text &" </span></li> "
Html_code = Html_code & "<li id=" "ct" "><span id=" "C1" "> Content </span><span id=" "C2" ">" & Temp_news.item (5). Text & "</span><span id=" "C3" "> Publish Time:" & Temp_news.item (2). Text & "clicks:" & Temp_news.item (6). Text & "</span></li>"
Next
Html_code = Html_code & "</div>"
Response.Write Html_code
%>

<form id= "Form1" Name= "Form1" method= "Post" action= "save_db.asp" >
<p> News title: <input type= "text" Name= "tt"/></p>
<p> News Author: <input type= "text" Name= "PT"/></p>
<p> news content: <textarea name= "CT" cols= "rows=" ></textarea></p>
<p><input type= "Submit" name= "Submit" value= "submitted"/><input "reset" type= "name=" Submit2 "reset" value= </p>
</form>

---------------------------------------post.asp---------------------------------------

The last is save_db.asp, which writes XML pages:

---------------------------------------save_db.asp---------------------------------------
<%
' TT author PT releases People CT news content
tt = Trim (Request.Form ("tt")) ' trim to space
PT = Trim (Request.Form ("PT"))
ct = Trim (request.form ("CT"))
Time_now = Now ()
' Catch time, as a different search for news items
D_y = year (Time_now)
D_m = month (Time_now)
D_d = Day (Time_now)
D_HR = Hour (Time_now)
D_mi = Minute (Time_now)
D_se = Second (Time_now)
PS: Why use date time to make news difference? Let's assume that this post backstage, only one of you can access, then you can be sure of something: at the same time, only one article can be published.
' Of course, the website platform is for multi-user, how to make more scientific and reasonable data index? Well, you are interested, I will discuss it separately in the future. Just use this for a while!
IDX = d_y & d_m & d_d & d_hr & D_mi & D_se
  
' In the middle of the check and judgment, I skipped over and looked directly into the XML document.
If TT = "" or pt = "" or ct = "" Then
msg = "News headlines, publishers, and content are required items." "
Response.Write "<script>alert (" "& msg &" \ n \ nthe page will automatically return ... ""); window.location = ' post.asp '; </script > "
Else
SourceFile = Server.MapPath ("Nndb.xml")
Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
Objxml.load (sourcefile)

If objXML.parseError.ErrorCode <> 0 Then
Objxml.loadxml "<?xml version=" "1.0" "encoding=" "GB2312" "? ><data><news></news></data > "
End If
    
Set objrootlist = ObjXML.documentElement.selectSingleNode ("News")
    
If Objrootlist.haschildnodes Then
id = objRootlist.lastChild.firstChild.text + 1
Else
Id=1
End If
    
Set Olistnode = ObjXML.documentElement.selectSingleNode ("News"). AppendChild (Objxml.createelement ("item"))
Set Odetailsnode = Olistnode.appendchild (objxml.createelement ("id"))
Odetailsnode.text = ID
Set Odetailsnode = Olistnode.appendchild (objxml.createelement ("index"))
Odetailsnode.text = idx
Set Odetailsnode = Olistnode.appendchild (Objxml.createelement ("Time"))
Odetailsnode.text = Time_now
Set Odetailsnode = Olistnode.appendchild (objxml.createelement ("title"))
Odetailsnode.text = TT
Set Odetailsnode = Olistnode.appendchild (Objxml.createelement ("poster"))
Odetailsnode.text = PT
Set Odetailsnode = Olistnode.appendchild (objxml.createelement ("content"))
Odetailsnode.text = objxml.createcdatasection (CT). Text
Set Odetailsnode = Olistnode.appendchild (Objxml.createelement ("hit"))
Odetailsnode.text = 0
    
Objxml.save (sourcefile)
    
Set Objxml = Nothing
    
msg = "News information has been successfully written." "
Response.Write "<script>alert (" "& msg &" \ n \ nthe page will automatically return ... ""); window.location = ' post.asp '; </script > "
End If
%>

---------------------------------------save_db.asp---------------------------------------

OK, so, three pages of files, basically finished. A small release system is basically formed, on this basis, can develop a lot of things, such as simple message book and so on.

A simple explanation:

Within the post.asp page, the Style section is the CSS stylesheet for making a news list, and you don't need to focus on it. The next thing is to create a DOM object and then read the file. The second half of the paragraph, is an input form.

Today's focus is: save_db.asp.

The first is Objxml.loadxml "<?xml version=" "1.0" "encoding=" "GB2312" "? ><data><news></news></ Data> ", here is the choice to read the XML document <data><news> start to </news></data> end, the middle part of the elements, should be easy to understand.

We then read all the elements under the news tag separately and return the first element. The HasChildNodes method is used to check whether a child node exists under the selected node. Yes, the table index number of the new news is formed according to the value of the first node in the last <item> node, +1. Otherwise, it means that there are no child nodes within the node, so the table index naturally starts at 1. This is not a science, we will not do a detailed discussion, and the index of the news data, as the focus of future discussions (personally, this is not the best practice, there are some serious loopholes.) )。

Then, it's the play. Set Olistnode = ObjXML.documentElement.selectSingleNode ("News"). AppendChild ("item"), which actually contains a two-step operation, first create a new node from the root node, and then create an element named item to become the node ... Why? Uh...... Can not say ah ... Then you create the child nodes under the item node, and the contents of the node in turn. Last written to the XML document, a simple write has been completed. There's not much to discuss, but here's a brief description of the objects and methods of the DOM, so you know.

The DocumentElement property confirms the root (root) node of the XML file.
selectSingleNode returns the first node that conforms to the style.
The Loadxml method loads a fragment of an XML file or string.
HasChildNodes method if the specified node has one or more child nodes, the return value is true.
The CreateElement method creates an element with the specified name.
The AppendChild method plus a node is treated as the last child node of the specified node.
The Createcdatasection method creates a CDATA that contains specific data.

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.