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

Mar 25, 2017 am 10:36 AM

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实现做通讯录的索引滑动显示效果和滑动显示锚点效果

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

How to set up address book blacklist on Xiaomi Mi 14? How to set up address book blacklist on Xiaomi Mi 14? Feb 24, 2024 pm 01:25 PM

When it comes to which mobile phone is the most popular recently, many people will probably think of the Xiaomi 14 which was just released. The performance configuration of this mobile phone is very good, and the user experience is also very good. In order to facilitate everyone's daily use, the editor will introduce it to you. Let’s take a look at how to set up the address book blacklist on Xiaomi Mi 14. Let’s take a look at the related content together! How to set up address book blacklist on Xiaomi Mi 14? Open the "Phone" app on the home screen of your phone, then find the "Contacts" option in the bottom navigation bar and click to enter. Browse the contact list, find the contact you want to add to the blacklist, and then press and hold the contact's avatar or name. After a while, a menu will pop up for you to choose an operation. Select "Blacklist this contact" or a similar option in the pop-up menu. The system will display the correct

How Python parses XML files How Python parses XML files Aug 09, 2023 am 11:48 AM

How Python parses XML files XML (eXtensibleMarkupLanguage) is a markup language used to represent structured data. When processing XML data, we often need to parse the XML file to extract the required information. Python provides many libraries and modules to parse XML files, such as ElementTree, lxml, etc. This article will introduce how to use Python to parse XML files, with code examples. In Python,

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

See all articles