Making Address Book _xml/rss with XML data island and Dom

Source: Internet
Author: User
Tags data structures dns2

In general, if you want to provide the site with an address book program, the need to use CGI combined with the background database technology, the requirements of the Web server is relatively high, in many do not provide database functionality of the virtual host can not even achieve. Of course, we can also use TXT text to replace the database, but txt text is more difficult to operate, we have to read a row of judgment, but also to use a delimited string to implement field separation, can not perform complex operations.
Now we can use Extensible Markup Language (XML) to store the data in the Address book, which reflects the advantages of XML: The structured approach to presentation data is helpful for saving files with many relational data structures.

First, the basic principle:
In Microsoft Internet Explorer 5.0 and later, we can use XML elements to create data islands that are referenced by HTML pages or contain XML data that can be included in an HTML file or included in an external file. Using XML data islands allows us to avoid the hassle of writing complex scripts. The DOM can parse an XML document, all individuals in a document, such as elements, entities, attributes, and so on, can be represented by the object model, the logical structure of the entire document resembles a tree, the resulting object model is the node of the tree, each object contains both methods and attributes, and DOM provides many ways to find nodes. With DOM, developers can dynamically create XML, traverse documents, add (delete/modify) document content, and Dom provides APIs that are independent of programming languages, so implementations of different parsers may differ for interfaces that are not explicitly defined in some DOM standards.

Second, the specific process is:
1. The definition XML file is as follows:
<?xml version= "1.0" encoding= "gb2312"?>
< China Computer World Publishing Service Company Communications >
< computer World contactid= "2" >
< department name > Computer Room </Department name >
< phone number >139</phone number >
< e-mail >fsdos@163.net</email >
</Computer World >
</China Computer World Publishing Service Co., Ltd communication >
Save the XML document as a Tele.xml file, and empty the field contents in the XML document as the initialization framework data and save as a newid.xml file.
2. The client loads the XML document and binds the XML file to the table by datasrc= ' #xmldso ' in the table where the address book is placed, and the Datasrc property is actually implemented by adding a # to the id attribute of the XML element to be processed. So we can specify the field to be displayed in the middle of TD element;
3, use the DOM technology to add, delete the record operation of the address book;
4, through the XMLHTTP protocol to connect to the server, save the XML document.

Three, XML DOM programming Brief introduction:
1, Client dom.htm page:
<xml id=xmldso src= "Tele.xml" ></XML>
<xml id=newid></xml> <!--load XML data-->
<script language=javascript>
Newid.async = false;
Newid.load ("Newid.xml");
Increase the record;
function Addid () {
var doc=xmldso. XmlDocument
var rootnode=doc.documentelement
var sortnode = Rootnode.selectnodes ("//Department name")
var CurrentID = sortnode.length-1
var cc=sortnode.item (currentid). text;
if ((cc== "not yet entered") | | (cc== ""))
{
Alert ("Please fill in the last line of data and add a new record!") ");
}
Else
{
var node= newid.documentElement.childNodes (0). CloneNode (True);
var contactid=parseint (Sortnode.item (CurrentID). Parentnode.getattribute ("ContactID")) +1;
Node.setattribute ("ContactID", ContactID);
Xmldso.documentElement.appendChild (node);
}
}
Delete a record
function Delid (WHICHFLD) {
var sortnode = Xmldso.selectsinglenode ("//Computer World [@contactID = '" +whichfld+ "]");
if (sortnode.parentnode.childnodes.length>1) sortNode.parentNode.removeChild (Sortnode);
}
</SCRIPT>
<script language= "VBScript" >
Sub cc_onmouseup ' Save record;
Dim Objxml, objxsl, Objfso,strfile, strFileName, Strxsl,strurl,theform
Set SAVEXMLDOC=XMLDSO. XmlDocument
Strurl= "Dns2.asp"
Set Objxml = CreateObject ("Microsoft.XMLHTTP") ' creates the XMLHTTP component of MS;
Objxml.open "POST", Strurl,false ' using post submission method;
Objxml.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Objxml.send Savexmldoc ' send information to save XML data;
' Xmlget = Objxml.responsebody ' Wait a moment, get the result of the server-side return;
MsgBox "Save success!" "
Set Objxml = Nothing
End Sub
</SCRIPT>
<center><b> Computer World----Communication record </b><br><br>
<table id= "TABLE" datasrc= ' #xmldso ' BORDER cellpadding=3>
Data binding for <!---->
<THEAD><TH> number </TH><TH> department name </TH><TH> phone number </TH><TH> e-mail </th ></THEAD>
<TR>
<td><acronym title= ' Click to delete the record ' ><input Type=button size=4 datafld= ' ContactID ' onclick= ' DelID ( This.value) "></acronym></TD>
<td><input type=text datafld= "department name" ></TD>
<td><input type=text datafld= "phone number" ></TD>
<td><input type=text datafld= "e-mail" ></TD>
</TR>
</TABLE>
<input Type=button name=dd id=dd value= "add Record" onmouseover= "This.focus ()" onmousedown= "Addid ();" >
<input Type=button name=cc id=cc value= "Save" ></center></BODY></HTML>

2, server-side dns2.asp program is relatively simple, after receiving the XML data, create a file object, save to Tele.xml:
<
Set Receiveddoc = CreateObject (" Microsoft.XMLDOM ")" Creates an XML DOM instance;
Receiveddoc.async=false
receiveddoc.load Request receives XML data;
Set files= Server.CreateObject ("Scripting.FileSystemObject")
Set numtxt=files. CreateTextFile (Server.MapPath ("Tele.xml"), True)
Numtxt. WriteLine (replace (receiveddoc.xml, "?>", "encoding=", "gb2312")) ' writes XML data to the file
?>. Close
Response.Write receiveddoc.xml
>

3, the actual use of the process, but also need to add a display Address Book page index.htm, in fact, is the above dom.htm simplified version, remove all add, delete, modify and save functions, only in the table cell with the label display data:
<HTML> <body bgcolor= #a1bae6 >
<xml id=xmldso src= "Tele.xml" ></XML>
<center><b> Computer World----Communications </b><br><br>
<table id= "TABLE" datasrc= ' #xmldso ' BORDER cellpadding=3>
<THEAD><TH> number </TH><TH> department name </TH><TH> phone number </TH><TH> e-mail </th >
</THEAD>
<TR>
<td><label datafld= "ContactID" ></label></TD>
<td><label datafld= "department name" ></label></TD>
<td><label datafld= "phone number" > </label></TD> 
<td><label datafld= "e-mail" ></label></TD>
</tr >
</TABLE>
</center></BODY></HTML>

The advantages of using XML data island combined with DOM technology:
1, the first, of course, is the benefits of XML itself. XML breaks the monopoly of the tag definition, you can customize the field name, in the XML file used in this article, even the field names can be Chinese, the data is very simple and clear, because it carries information is not the description of the display, but the semantics of information, greatly enhance the readability of the document. Using XML also facilitates the transmission of information between different systems.
2, the XML data island allows users to access and manipulate the dataset on the client, without frequent interaction with the server, which is helpful to reduce the load on the server. At the same time, because of the characteristics of the XML data island, it makes the data operation in the client very simple and reduces the programming quantity.
3. Dom enforces the use of tree models to access information in XML documents, which is quite effective because XML is essentially a hierarchical structure. With the DOM interface, an application can access any part of the data in an XML document at any time and is quite flexible to control.
4, the use of XMLHTTP objects to transfer XML data to the server, the client page without flashing refresh phenomenon.

This program runs on the IIS5.0 and IE5.0 based on the Windows2000 platform. In the actual use of the process, can also use the DOM combined with XSL technology for the address Book to add sorting, format conversion and data lookup functions, using the XML data Island DataPageSize properties and PreviousPage, NextPage function for the address book to add paging function, Dynamically validate Address book data using DTDs and XML schemas.

------------------------the end----------------------


Attached: (Full source program)
****************************************************************************
First, index.htm (show newsletter):
<xml id=xmldso src= "Tele.xml" ></XML>
<center><b> Computer World----Newsletter </b><br><br>
<table id= "TABLE" datasrc= ' #xmldso ' BORDER cellpadding=3>
<THEAD><TH> number </TH><TH> department name </TH><TH> phone number </TH><TH> e-mail </th >
</THEAD>
<TR>
<td><label datafld= "ContactID" ></label></TD>
<td><label datafld= "department name" ></label></TD>
<td><label datafld= "Phone number" ></label></TD>
<td><label datafld= "e-mail" ></label></TD>
</TR>
</TABLE>
</center></BODY></HTML>
****************************************************************************
Ii. dom.htm (online edit newsletter):
<xml id=xmldso src= "Tele.xml" ></XML>
<xml id=newid></xml>
<script language=javascript>
Newid.async = false;
Newid.load ("Newid.xml");
function Addid () {
var doc=xmldso. XmlDocument
var rootnode=doc.documentelement
var sortnode = Rootnode.selectnodes ("//Department name")
var CurrentID = sortnode.length-1
var cc=sortnode.item (currentid). text;
if ((cc== "not yet entered") | | (cc== ""))
{
Alert ("Please fill in the last line of data and add a new record!") ");
}
Else
{
var node= newid.documentElement.childNodes (0). CloneNode (True);
var contactid=parseint (Sortnode.item (CurrentID). Parentnode.getattribute ("ContactID")) +1;
Node.setattribute ("ContactID", ContactID);
Xmldso.documentElement.appendChild (node);
}
}
function Delid (WHICHFLD) {
var sortnode = Xmldso.selectsinglenode ("//Computer World [@contactID = '" +whichfld+ "]");
if (sortnode.parentnode.childnodes.length>1) sortNode.parentNode.removeChild (Sortnode);
}
</SCRIPT>
<script language= "VBScript" >
Sub cc_onmouseup ' triggers when you click the ' Save ' button;
Dim Objxml, objxsl, Objfso,strfile, strFileName, Strxsl,strurl,theform
Set SAVEXMLDOC=XMLDSO. XmlDocument
Strurl= "Dns2.asp"
Set Objxml = CreateObject ("Microsoft.XMLHTTP") ' creates the XMLHTTP component of MS;
Objxml.open "POST", Strurl,false ' using post submission method;
Objxml.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Objxml.send savexmldoc ' Send message
' Xmlget = Objxml.responsebody ' Wait a moment, get the result of the server-side return;
MsgBox "Save success!" "
Set Objxml = Nothing
End Sub
</SCRIPT>
<center><b> Computer World----Communication record </b><br><br>
<table id= "TABLE" datasrc= ' #xmldso ' BORDER cellpadding=3>
<THEAD>
<TH> number </TH>
<TH> Department Name </TH>
<TH> Phone number </TH>
<TH> Email </TH>
</THEAD>
<TR>
<td><acronym title= ' Click to delete the record ' ><input Type=button size=4 datafld= ' ContactID ' onclick= ' DelID ( This.value) "></acronym></TD>
<td><input type=text datafld= "department name" ></TD>
<td><input type=text datafld= "phone number" ></TD>
<td><input type=text datafld= "e-mail" ></TD>
</TR>
</TABLE>
<input Type=button name=dd id=dd value= "add Record" onmouseover= "This.focus ()" onmousedown= "Addid ();" >
<input Type=button name=cc id=cc value= "Save" ></center></BODY></HTML>
****************************************************************************
Third, dns2.asp (backstage saves the correspondence record):
<%
Set Receiveddoc = CreateObject ("Microsoft.XMLDOM")
Receiveddoc.async=false
Receiveddoc.load Request
Set files=server.createobject ("Scripting.FileSystemObject")
Set Numtxt=files. CreateTextFile (Server.MapPath ("Tele.xml"), True)
Numtxt. WriteLine (replace (receiveddoc.xml, "?>", "encoding=" "gb2312" "?>"))
Numtxt. Close
Response.Write Receiveddoc.xml
%>
****************************************************************************
Iv. Tele.xml (Address Book XML document):
<?xml version= "1.0" encoding= "gb2312"?>
< China Computer World Publishing Service Company Communications >
< computer World contactid= "1" >
< department name > telephone Switchboard </Department name >
< phone number >010-68130909</phone number >
< e-mail >webmaster@ccw.com.cn</email >
</Computer World >
</China Computer World Publishing Service Co., Ltd communication >
****************************************************************************
V. Newid.xml (Address Book XML initialization document):
<?xml version= "1.0" encoding= "gb2312"?>
< China Computer World Publishing Service Company Communications >
< computer World contactid= "1" >
< department name > not yet entered </Department name >
< phone number > confidential </telephone number >
< email > Confidential </Email >
</Computer World >
</China Computer World Publishing Service Co., Ltd communication >

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.