Home > Backend Development > XML/RSS Tutorial > Detailed explanation of code examples for creating address book through XML data island and Dom

Detailed explanation of code examples for creating address book through XML data island and Dom

黄舟
Release: 2017-03-25 10:36:08
Original
1595 people have browsed it

Generally, if you want to provide an address book program for a website, you need to use CGI combined with background database technology. This has relatively high requirements for the WEB server 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 more difficult to operate. We must read and judge line by line, and also use delimited strings to separate fields, and complex operations cannot be performed.
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, which is very useful for saving files with many relational data structures. help.

1. Basic principles:
In Microsoft Internet Explorer 5.0 and later versions, we can use XML elements to create data islands. Data islands are XML data that are referenced or included in HTML pages. XML data It can be included in an HTML file or in an external file. Using XML data islands can save us from the trouble of writing complex scripts. DOM can parse XML documents. All 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 also contains In addition to methods and attributes, DOM provides many methods for finding nodes. Using DOM, developers can dynamically create XML, traverse documents, and add (delete/modify) document content. The API provided by DOM has nothing to do with programming languages, so there are no clearly defined interfaces in some DOM standards, and the implementation methods of different parsers May vary.

2. The specific process is:
1. Define the XML file as follows:

<?xml version="1.0" encoding="gb2312"?> 
    <中国计算机世界出版服务公司通信录> 
      <计算机世界 contactID="2"> 
        <部门名称>计算机室</部门名称> 
        <电话号码>139</电话号码> 
        <电子邮件>fsdos@163.net</电子邮件> 
      </计算机世界> 
    </中国计算机世界出版服务公司通信录>
Copy after login

Save the above XML document as a tele.xml file, and at the same time, save the above XML document as Leave the field content blank as initialization frame data and save it as a newid.xml file.
2. The client loads the XML document and binds the XML file to the table through DATASRC='#xmldso' in the table where the address book is placed. The DATASRC attribute is actually passed through the ID attribute of the XML element to be processed. This is achieved by adding # in front, so we can specify the specific fields that need to be displayed in the middle of the TD element;
3. Use DOM technology to add and delete records in the address book;
4. Connect to the address book through the xmlhttp protocol Server, saves XML documents.

3. Brief description of XML DOM programming:
1. Client dom.htm page:

<HTML><BODY bgColor=#a1bae6> 
<XML id=xmldso src="tele.xml"></XML> 
<XML id=newid></XML> <!--加载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("//部门名称") 
var currentid = sortNode.length-1 
var cc=sortNode.item(currentid).text; 
if ((cc=="尚未输入")||(cc=="")) 
{ 
alert("请将最后一行数据填写完毕后再增加新的记录!"); 
} 
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("//计算机世界[@contactID=&#39;"+whichFld+"&#39;]"); 
if (sortNode.parentNode.childNodes.length>1) sortNode.parentNode.removeChild(sortNode);  
} 
</SCRIPT> 
<script language="vbscript"> 
Sub cc_onmouseup &#39;保存记录; 
Dim objXML, objXSL, objfso,strFile, strFileName, strXSL,strURL,TheForm 
set SaveXMLDoc=xmldso.XMLDocument 
strURL="dns2.asp" 
Set objXML = CreateObject("Microsoft.XMLHTTP") &#39;创建MS的XMLHTTP组件; 
objXML.Open "post",strURL,false &#39;采用Post提交方式; 
objXML.setrequestheader "content-type","application/x-www-form-urlencoded" 
objXML.send SaveXMLDoc &#39; 发送信息,保存XML数据; 
&#39;xmlGet = objXML.responsebody &#39;稍等片刻后,得到服务器端传回来的结果; 
msgbox "保存成功!"  
Set objXML = Nothing 
end sub  
</SCRIPT> 
<center><b>计算机世界----通信录</b><br><br> 
<TABLE id="table" DATASRC=&#39;#xmldso&#39; BORDER CELLPADDING=3> 
<!--进行数据绑定--> 
<THEAD><TH>编号</TH><TH>部门名称</TH><TH>电话号码</TH><TH>电子邮件</TH></THEAD> 
<TR> 
<TD><acronym title=&#39;点击即可删除该记录&#39;><INPUT TYPE=button size=4 DATAFLD="contactID" onclick="delID(this.value)"></acronym></TD> 
<TD><INPUT TYPE=TEXT DATAFLD="部门名称"></TD> 
<TD><INPUT TYPE=TEXT DATAFLD="电话号码"></TD>  
<TD><INPUT TYPE=TEXT DATAFLD="电子邮件"></TD> 
</TR> 
</TABLE> 
<INPUT TYPE=BUTTON name=dd id=dd VALUE="增加记录" onmouSEOver="this.focus()" onmousedown="addID();"> 
<INPUT TYPE=BUTTON name=cc id=cc VALUE="保存"></center></BODY></HTML>
Copy after login

2. The server-side dns2.asp program is relatively simple. After receiving the XML data , create a file object and save it to tele.xml:

< 
Set ReceivedDoc = CreateObject("Microsoft.XMLDOM") &#39;创建 XML DOM实例; 
ReceivedDoc.async=False 
ReceivedDoc.load Request &#39;接收XML数据; 
Set files=Server.CreateObject("Scripting.FileSystemObject") 
Set numtxt=files.CreateTextFile(Server.MapPath("tele.xml"),True) 
numtxt.WriteLine(replace(ReceivedDoc.xml,"?>"," encoding=""gb2312""?>")) &#39;将XML数据写入文件 
numtxt.Close 
response.write ReceivedDoc.xml 
>
Copy after login

3. During actual use, you also need to add a web page index.htm that displays the address book, which is actually a simplified version of the above dom.htm. Remove all add, delete, modify and save functions, and only use LABEL to display data in table cells:

<HTML><BODY bgColor=#a1bae6> 
<XML id=xmldso src="tele.xml"></XML> 
<center><b>计算机世界----通信录</b><br><br> 
<TABLE id="table" DATASRC=&#39;#xmldso&#39; BORDER CELLPADDING=3> 
<THEAD><TH>编号</TH><TH>部门名称</TH><TH>电话号码</TH><TH>电子邮件</TH> 
</THEAD> 
<TR> 
<TD><label DATAFLD="contactID"></label></TD> 
<TD><label DATAFLD="部门名称"></label></TD> 
<TD><label DATAFLD="电话号码"></label></TD>  
<TD><label DATAFLD="电子邮件"></label></TD> 
</TR> 
</TABLE> 
</center></BODY></HTML>
Copy after login

4. Advantages of using XML data island combined with Dom technology:
1. First of all, of course XML itself brings benefits. XML breaks the monopoly of tag definitions. You can customize field names. In the XML file used in this article, even the field names can be in Chinese. The data is very simple and clear, because the information it carries is not a description on the display, but a description on the display. It is the semantic meaning of the information, which greatly enhances the readability of the document. Using XML also facilitates the transfer of information between different systems.
2. XML data island allows users to access and manipulate data sets on the client without frequent interaction with the server, 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, data operations on the client are very simple and the amount of programming is reduced.
3. DOM forces the use of a tree model to access information in XML documents. Since XML is essentially a hierarchical structure, this description method is quite effective. Through the DOM interface, the application can access any part of the data in the XML document at any time, and the control is quite flexible.
4. Use the xmlhttp object to transmit XML data to the server, and the client page will refresh without flickering.

This program runs successfully on IIS5.0 and IE5.0 based on Windows2000 platform. In the actual application process, you can also use DOM combined with XSL technology to add functions such as sorting, format conversion, and data search to the address book. Use the datapagesize attribute of the XML data island and the PReviousPage and nextPage functions to add paging functions to the address book. Use DTD and XML Schema dynamically validates address book data.

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

Attachment: (all source programs)
****************************** **********************************************
one , index.htm (display address book):

<HTML><BODY bgColor=#a1bae6> 
<XML id=xmldso src="tele.xml"></XML> 
<center><b>计算机世界----通迅录</b><br><br> 
<TABLE id="table" DATASRC=&#39;#xmldso&#39; BORDER CELLPADDING=3> 
<THEAD><TH>编号</TH><TH>部门名称</TH><TH>电话号码</TH><TH>电子邮件</TH> 
</THEAD> 
<TR> 
<TD><label DATAFLD="contactID"></label></TD> 
<TD><label DATAFLD="部门名称"></label></TD> 
<TD><label DATAFLD="电话号码"></label></TD>  
<TD><label DATAFLD="电子邮件"></label></TD> 
</TR> 
</TABLE> 
</center></BODY></HTML>
Copy after login

********************************** *******************************************

two , dom.htm (online editing address book):

<HTML><BODY bgColor=#a1bae6> 
<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("//部门名称") 
var currentid = sortNode.length-1 
var cc=sortNode.item(currentid).text; 
if ((cc=="尚未输入")||(cc=="")) 
{ 
alert("请将最后一行数据填写完毕后再增加新的记录!"); 
} 
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("//计算机世界[@contactID=&#39;"+whichFld+"&#39;]"); 
if (sortNode.parentNode.childNodes.length>1) sortNode.parentNode.removeChild(sortNode);  
} 
</SCRIPT> 
<script language="vbscript"> 
Sub cc_onmouseup &#39;当点击“保存”按钮时触发; 
Dim objXML, objXSL, objFSO,strFile, strFileName, strXSL,strURL,TheForm 
set SaveXMLDoc=xmldso.XMLDocument 
strURL="dns2.asp" 
Set objXML = CreateObject("Microsoft.XMLHTTP") &#39;创建MS的XMLHTTP组件; 
objXML.Open "post",strURL,false &#39;采用Post提交方式; 
objXML.setrequestheader "content-type","application/x-www-form-urlencoded" 
objXML.send SaveXMLDoc &#39; 发送信息 
&#39;xmlGet = objXML.responsebody &#39;稍等片刻后,得到服务器端传回来的结果; 
msgbox "保存成功!"  
Set objXML = Nothing 
end sub  
</SCRIPT> 
<center><b>计算机世界----通信录</b><br><br> 
<TABLE id="table" DATASRC=&#39;#xmldso&#39; BORDER CELLPADDING=3> 
<THEAD> 
<TH>编号</TH> 
<TH>部门名称</TH> 
<TH>电话号码</TH> 
<TH>电子邮件</TH> 
</THEAD> 
<TR> 
<TD><acronym title=&#39;点击即可删除该记录&#39;><INPUT TYPE=button size=4 DATAFLD="contactID" onclick="delID(this.value)"></acronym></TD> 
<TD><INPUT TYPE=TEXT DATAFLD="部门名称"></TD> 
<TD><INPUT TYPE=TEXT DATAFLD="电话号码"></TD>  
<TD><INPUT TYPE=TEXT DATAFLD="电子邮件"></TD> 
</TR> 
</TABLE> 
<INPUT TYPE=BUTTON name=dd id=dd VALUE="增加记录" onmouseover="this.focus()" onmousedown="addID();"> 
<INPUT TYPE=BUTTON name=cc id=cc VALUE="保存"></center></BODY></HTML>
Copy after login

******************************** ************************************************

3. dns2.asp (save address book in the background):

<% 
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 
%>
Copy after login

************************************ *********************************************

4. tele.xml (address book XML document):

<?xml version="1.0" encoding="gb2312"?> 
<中国计算机世界出版服务公司通信录> 
<计算机世界 contactID="1"> 
<部门名称>电话总机</部门名称> 
<电话号码>010-68130909</电话号码> 
<电子邮件>webmaster@ccw.com.cn</电子邮件> 
</计算机世界> 
</中国计算机世界出版服务公司通信录>
Copy after login

****************************************************************************

五、newid.xml(通讯录XML初始化文档):

<?xml version="1.0" encoding="gb2312"?> 
<中国计算机世界出版服务公司通信录> 
<计算机世界 contactID="1"> 
<部门名称>尚未输入</部门名称> 
<电话号码>保密</电话号码> 
<电子邮件>保密</电子邮件> 
</计算机世界> 
</中国计算机世界出版服务公司通信录>
Copy after login

 以上就是用XML数据岛结合Dom制作通讯录的代码实例详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章:

php实现在线通讯录功能(附源码),通讯录源码

详解HTML5通讯录获取指定多个人的信息的示例代码

js实现做通讯录的索引滑动显示效果和滑动显示锚点效果

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template