SoFunction
Updated on 2025-04-08

Use XML data island to combine Dom to create address books

Generally speaking, if you want to provide a address book program for a website, you need to use CGI and backend database technology, which has high requirements for WEB servers and cannot even be implemented on many virtual hosts that do not provide database functions. Of course, we can also use TXT text to replace the database, but TXT text is relatively difficult to operate. We must read and judge line by line, and also use separate strings to achieve field separation, so we cannot perform complex operations.
Now we can use "Extensible Markup Language (XML)" to save address book data, thus reflecting the advantages of XML: a structured method of expressing data is very helpful for saving files with many relational data structures.

1. Basic principles:
In Microsoft Internet Explorer 5.0 and later, we can use XML elements to create data islands. Data islands are XML data referenced or contained by HTML pages. XML data can be included in HTML files or in an external file. Using XML data islands can save us from the trouble of writing complex scripts. DOM can parse XML documents, and all individuals such as elements, entities, attributes, etc. in the document can be represented by an object model. The logical structure of the entire document is similar to a tree. The generated object model is the node of the tree. Each object contains both methods and attributes. DOM provides many methods to find nodes. Using DOM, developers can dynamically create XML, traverse documents, and add (delete/modify) document content. The API provided by DOM is not related to the programming language, so for some DOM standards, the implementation methods of different parsers may be different.

2. The specific process is:
1. Defining the XML file is as follows:
<?xml version="1.0" encoding="gb2312"?>
<Contact of China Computer World Publishing Service Company>
<Computer World contactID="2">
<Department Name>Computer Room</Department Name>
<Phone Number>139</Phone Number>
<Email>fsdos@</Email>
</Computer World>
</Tels of China Computer World Publishing Service Company>
Save the above XML document as a file. At the same time, empty the field content in the above XML document as initialization framework data, and save it as a file.
2. The client loads the XML document and binds the XML file into the table in which the address book is placed using DATASRC='#xmldso'. The DATASRC attribute is actually implemented by adding # in front of the ID attribute of the XML element to be processed, so we can specify the specific fields to be displayed in the middle of the TD element;
3. Use DOM technology to add and delete address books;
4. Connect to the server through the XMLHTTP protocol and save the XML document.

3. Brief description of XML DOM programming:
1. Client page:
<HTML><BODY bgColor=#a1bae6>
<XML id=xmldso src=""></XML>
<XML id=newid></XML> <!--Load xml data-->
<SCRIPT Language=JavaScript>
= false;
("");
//Add records;
function addID(){
var doc=
var rootnode=
var sortNode = ("//Department Name")
var currentid = -1
var cc=(currentid).text;
if ((cc=="Not entered yet")||(cc==""))
{
alert("Please fill in the last line of data before adding a new record!");
}
else

var node= (0).cloneNode(true);
var contactID=parseInt((currentid).("contactID"))+1; 
("contactID",contactID); 
(node);
}
}
//Delete the record
function delID(whichFld){
var sortNode = ("//Computer World[@contactID='"+whichFld+"']");
if (>1) (sortNode); 
}
</SCRIPT>
<script language="vbscript">
Sub cc_onmouseup 'Save record;
Dim objXML, objXSL, objFSO,strFile, strFileName, strXSL,strURL,TheForm
set SaveXMLDoc=
strURL=""
Set objXML = CreateObject("") 'Create XMLHTTP component for MS;
"post",strURL,false 'Use Post submission method;
"content-type","application/x-www-form-urlencoded"
SaveXMLDoc ' Send information and save XML data;
'xmlGet = 'After waiting for a while, you will get the result sent back from the server;
msgbox "Save successfully!"
Set objXML = Nothing
end sub 
</SCRIPT>
<center><b>Computer World---Tels</b><br><br>
<TABLE DATASRC='#xmldso' BORDER CELLPADDING=3>
<!--Make data binding->
<THEAD><TH>Number</TH><TH>Department Name</TH><TH>Telephone Number</TH><TH>Email</TH></THEAD>
<TR>
<TD><acronym title='Click to delete the record'><INPUT TYPE=button size=4 DATAFLD="contactID" onclick="delID()"></acronym></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Department Name"></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Telect Number"></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Email"></TD>
</TR>
</TABLE>
<INPUT TYPE=BUTTON name=dd id=dd VALUE="Add record" onmouseover="()" onmousedown="addID();">
<INPUT TYPE=BUTTON name=cc id=cc VALUE="Save"></center></BODY></HTML>

2. The server-side program is relatively simple. After receiving XML data, create a file object and save it to:
<
Set ReceivedDoc = CreateObject("") 'Create XML DOM instance;
=False
Request 'Receive XML data;
Set files=("")
Set numtxt=((""),True)
(replace(,"?>"," encoding=""gb2312"?>")) 'Write XML data to a file


>

3. During the actual use process, you also need to add a web page to display the address book. In fact, it is the simplified version above. All addition, deletion, modification and saving functions are removed, and only use LABEL to display data in table cells:
<HTML><BODY bgColor=#a1bae6>
<XML id=xmldso src=""></XML>
<center><b>Computer World---Tels</b><br><br>
<TABLE DATASRC='#xmldso' BORDER CELLPADDING=3>
<THEAD><TH>Number</TH><TH>Department Name</TH><TH>Telephone Number</TH><TH>Email</TH>
</THEAD>
<TR>
<TD><label DATAFLD="contactID"></label></TD>
<TD><label DATAFLD="Department Name"></label></TD>
<TD><label DATAFLD="Tel Number"></label></TD>
<TD><label DATAFLD="Email"></label></TD>
</TR>
</TABLE>
</center></BODY></HTML>

4. Advantages of using XML data island combined with Dom technology:
1. First of all, of course, the benefits brought by XML itself. XML breaks the monopoly of mark-definition. You can customize the field name. In the XML file used in this article, the field name can be in Chinese. The data is very simple and clear, because the information it carries is not the description on the display, but the semantics of the information, which greatly enhances the readability of the document. Using XML also facilitates the transmission of information between different systems.
2. XML data island allows users to access and manipulate data sets on the client side, and do not have to interact with the server frequently, which is very helpful in reducing the load on the server. At the same time, due to the characteristics of the XML data island itself, the data operation on the client is very simple and the programming amount is reduced.
3. DOM forces the use of tree models to access information in XML documents. Since XML is essentially a hierarchical structure, this description method is quite effective. Through the DOM interface, applications can access any part of the data in the XML document at any time, and are quite flexible to control.
4. Use the XMLhttp object to transmit XML data to the server, and the client page will be refreshed without flickering.

This program runs on IIS5.0 and IE5.0 based on Windows 2000 platform. In actual application, you can also use DOM combined with XSL technology to add sorting, format conversion and data search functions to address books, use the datapagesize attribute of XML data island and the previousPage and nextPage functions to add paging functions to address books, and use DTD and XML Schema to dynamically verify address books data.

 

------------------------THE END----------------------

 


Attachment: (all source programs)
****************************************************************************
1. (Show address book):
<HTML><BODY bgColor=#a1bae6>
<XML id=xmldso src=""></XML>
<center><b>Computer World---Transfer Record</b><br><br>
<TABLE DATASRC='#xmldso' BORDER CELLPADDING=3>
<THEAD><TH>Number</TH><TH>Department Name</TH><TH>Telephone Number</TH><TH>Email</TH>
</THEAD>
<TR>
<TD><label DATAFLD="contactID"></label></TD>
<TD><label DATAFLD="Department Name"></label></TD>
<TD><label DATAFLD="Tel Number"></label></TD>
<TD><label DATAFLD="Email"></label></TD>
</TR>
</TABLE>
</center></BODY></HTML>
****************************************************************************
2. (Edit address book online):
<HTML><BODY bgColor=#a1bae6>
<XML id=xmldso src=""></XML>
<XML id=newid></XML>
<SCRIPT Language=JavaScript>
= false;
("");
function addID(){
var doc=
var rootnode=
var sortNode = ("//Department Name")
var currentid = -1
var cc=(currentid).text;
if ((cc=="Not entered yet")||(cc==""))
{
alert("Please fill in the last line of data before adding a new record!");
}
else

var node= (0).cloneNode(true);
var contactID=parseInt((currentid).("contactID"))+1; 
("contactID",contactID); 
(node);
}
}
function delID(whichFld){
var sortNode = ("//Computer World[@contactID='"+whichFld+"']");
if (>1) (sortNode); 
}
</SCRIPT>
<script language="vbscript">
Sub cc_onmouseup 'Free when the "Save" button is clicked;
Dim objXML, objXSL, objFSO,strFile, strFileName, strXSL,strURL,TheForm
set SaveXMLDoc=
strURL=""
Set objXML = CreateObject("") 'Create XMLHTTP component for MS;
"post",strURL,false 'Use Post submission method;
"content-type","application/x-www-form-urlencoded"
SaveXMLDoc' Send Message
'xmlGet = 'After waiting for a while, you will get the result sent back from the server;
msgbox "Save successfully!"
Set objXML = Nothing
end sub 
</SCRIPT>
<center><b>Computer World---Tels</b><br><br>
<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()"></acronym></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Department Name"></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Telect Number"></TD>
<TD><INPUT TYPE=TEXT DATAFLD="Email"></TD>
</TR>
</TABLE>
<INPUT TYPE=BUTTON name=dd id=dd VALUE="Add record" onmouseover="()" onmousedown="addID();">
<INPUT TYPE=BUTTON name=cc id=cc VALUE="Save"></center></BODY></HTML>
****************************************************************************
3. (Save address book in the background):
<%
Set ReceivedDoc = CreateObject("")
=False
Request
Set files=("")
Set numtxt=((""),True)
(replace(,"?>"," encoding=""gb2312""?>"))


%>
****************************************************************************
4. (Display XML document):
<?xml version="1.0" encoding="gb2312"?>
<Contact of China Computer World Publishing Service Company>
<Computer World contactID="1">
<Department Name>Telephone Switchboard</Department Name>
<Tel number>010-68130909</Tel number>
<Email>webmaster@</email>
</Computer World>
</Tels of China Computer World Publishing Service Company>
****************************************************************************
5. (Door List XML initialization document):
<?xml version="1.0" encoding="gb2312"?>
<Contact of China Computer World Publishing Service Company>
<Computer World contactID="1">
<Department Name>Not entered yet</Department Name>
<Phone Number>Confidential</Phone Number>
<Email>Confidential</email>
</Computer World>
</Tels of China Computer World Publishing Service Company>