Application of XML in the ASP environment (recommended)

zhaozj2021-02-08  226

Application of XML in the ASP environment (recommended)

XML is known as the life of the future web. This article describes how to use the new XML document object model (DOM) to resolve and apply XML data through the ASP program. I. The ability of the document object model in the server parsing and applying XML to the developer has opened a new world. With the increasingness of XML applications, it is more important to process XML on the server. This article demonstrates the XML document processing process of the ASP application in the server, and the XML example used is a news document. We will introduce how to write a simple ASP page that displays the date of the news document and the number of news, and how to display the news header and the corresponding URL in the ASP page. Document Object Model is Document Object Model, referred to as DOM. The XML document object model of IE 5.0 provides full support for the programming interface described in the W3C DOM Core Specification (Level 1), and it also supports a series of XML related technologies, such as XSL, XSL mode matching, namespace, data type, etc. . The DOM is the most basic document model exported by the XML parser, which describes the XML document as a tree structure that is easy to navigate and use. The W3C's DOM core specification defines two groups of DOM programming interfaces. The first set of interfaces are required to write processing and using an XML document, and the second set of interface assisted developers makes the XML document easier. Providing the second set of interfaces is for convenience, which they are not required for XML applications. In ASP applications, it is quite simple to apply DOM on the server, but IE 5.0 must be installed on the server because many of the support components of the DOM are provided by IE. After installing IE, you can create a DOM object as needed in ASP applications: <% set objxml = server.createObject ("Microsoft.xmLDom")%> Second, processing XML (1) on the server After creating a DOM object, we can construct new XML documents themselves, or you can load an existing document. If it is loaded with an existing document, you can also choose to read the XML text string or open the XML document and load its content. In the examples of this article, we assume that there is already a recent news of the XML document MostRecentScriptingNews.xml on the server. Before loading the XML document, we should set the Async property of the DOM object to "false", which tells the DOM object to load an XML document is not asynchronous. This is very important, because after reading the XML document, we will start using it immediately. If the document is not fully loaded, try to access it will trigger an error.

<% Set objXML = Server.CreateObject ( "Microsoft.XMLDOM") objXML.async = FalseobjXML.Load (Server.MapPath ( "mostRecentScriptingNews.xml"))%> The following is our load XML document mostRecentScriptingNews.xml:

Copyright 1.0 WED, 03 Mar 1999 08:00:00 GMT Thu, 04 Mar 1999 03:37:03 GMT Linux car stereo system http://www.wired.com/news/news/technology/story/18236.html car stereo system < / linetext> ... According to News.com, HP will provide storage and computing services in a lease. http://www.news.com/news/item/0, 4,33202,00.html?st.ne.fd.mdh is taken from News The ParseError object of the .com DOM model contains the final parsing error message, which is useful for ASP page debugging and error control. After the document is loaded, check the ParseError object before continuing other operations to see if there is an error is a good habit: <% if objxml.parseerror.Errorcode <> 0 Then & Single; Processing Error End IF%> ParseError object provides an error Rich and valuable information: ParseError object Properties Description ErrorCode Error Code Filepos Errors Errors Absolute File Line LinePos Errors Row LinePOS Errors Character In This Row Reason Error Cause SrcText Errors Data URL Error XML The URL of the document is in this case, the XML document references a DTD (Document Type Definition, document type definition) file, which is more important in this. Here, if there is no error, the XML document must not only form a well format, but it must also legal for the specified DTD definition. After loading the XML document, you always check the ParseError object is a good program habit.

Second, processing XML (2) on the server now has a format well, legal document, let's see what this document is. DOM provides many ways to accurately analyze document content. Since the DOM describes the tree as a tree composed of a nature of the node (each node is constructed of one element and all of the sub-elements), the processing XML data actually comes down to process a series of node objects. Below we will use the getElementsBytagname method to get an element (or node) from the document. Our first goal is to find the release date of the news. Analyze the DTD file and we know that the release date is placed in the Pubdate node. A relatively simple way to access the node content is to first create a list object of all nodes in an XML document, and then traverse this list with loop until the Pubdate node is found. Since the DTD specifies that the Pubdate node cannot contain any child nodes, we can get the contents of the node through the Text property. <% Set objXML = Server.CreateObject ( "Microsoft.XMLDOM") Set objLst = Server.CreateObject ( "Microsoft.XMLDOM") objXML.async = FalseobjXML.Load (Server.MapPath ( "mostRecentScriptingNews.xml")) If objXML. parseError.errorCode <> 0 Then & single; processing error End IfSet objLst = objXML.getElementsByTagName ( "*") For i = 0 to (? objLst.length 1) If objLst.item (i) .nodeName = "pubDate" ThenStrDate = objLst .Item (i) .textexit forends ifdenxt%> Note In the above code we take "*" as a parameter of getElementsByTagname, at this time, getElementsByTagname will return a list of all elements (or nodes) in the document. Since we already have DTD, you can learn directly from DTD to the correct location of Pubdate, but it is also an efficient method through loop traversal documents as in the above example because the node list is a collection. Now we have received the issuance date of the news, let's take a look at how to calculate the number of news in the document. From the DTD definition of the document we know that news is stored in the ITEM node, each ITEM node in the document corresponds to a news. Obviously, we can use another loop similar to the above example, plus the counter each time ITEM node in the loop 1. However, we have a better way to extract this information, which is another method provided by the DOM. As in the above example, what we have to do is to create a list object containing all Item nodes, then get the number of nodes in the node list object through the length property, so that the number of news in the document: <% set objlst = Objxml .getelementsBytagname ("item") strnoofheadlines = Objlst.Length%> Many times we have to display some information from the XML document in the ASP page. The following example shows how to display news and its URL in the ASP page through the list of news nodes.

转载请注明原文地址:https://www.9cbs.com/read-1670.html

New Post(0)