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:
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.