Technically speaking, in the ASP environment, there are three main ways to read and manage XML text:
Create an MSXML object and load the XML document into the DOM;
Use server-side embedding (Server-Side Include, SSI);
Just like accessing other text files, use FileSystemObject to access XML documents;
The fourth method is to create a built-in data island on the client and explain the relevant content later.
1. Use DOM
In order to use DOM in ASP code, you need to create an instance of the Microsoft XML parser, which is instantiated like any other COM component, adding a few lines of standard code to the beginning of the page. This code creates an analyzer instance, loads the XML document to the DOM, and sets the root element (i.e. the document element) to the current node.
'Instatiate the XML Processor
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
Load the XML Document
objXML.load(Server.MapPath("mydata.xml")
Set the Document Element
Set objRootElement = objXML.documentElement
Before the XML document is loaded, the fourth step needs to be performed, which is to set the validateOnParse property to True, which ensures that the loaded document is a valid XML document. This can avoid the troubles that come later:
Instatiate the XML Processor
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
The processes should validate the document
objXML.validateOnParse = True
Load the XML Document
objXML.load(Server.MapPath("mydata.xml")
Set the Document Element
Set objRootElement = objXML.documentElement
Finally, there is an optional step, which also appears before loading. It requires the file to be loaded synchronously:
objXML.async = false
This says that when loading and verifying a considerable file takes some time. Another alternative is to ignore this step and allow asynchronous loading, which is the default case, once these initialization steps are completed, the XML document is loaded and ready to be processed. All important features of the DOM are configurable.
Of course, just like any COM object, after using it, remember to destroy it:
Set objXML = nothing
2. Server-side embedding
Server-side embedding can be used to insert XML document code into ASP pages.
3. Examples of using ASP code to process XML
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Dim sourceFile,source,rootElement,HTMLCode
sourceFile = Request.ServerVariables("APPL_PHYSICAL_PATH") & "xml/contacts.xml"
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.load sourceFile
set rootElement = source.documentElement
HTMLCode = HTMLCode & "<font size=4 face=verdana>"
HTMLCode = HTMLCode & rootElement.childNodes(0).text
HTMLCode = HTMLCode & "</font><p></p><font size=3 face=verdana><I>"
HTMLCode = HTMLCode & rootElement.childNodes(0).text
HTMLCode = HTMLCode & "</I></font><p></p><font size=3 face=verdana>"
HTMLCode = HTMLCode & rootElement.childNodes(0).text
HTMLCode = HTMLCode & "</font><p></p>"
response.write(HTMLCode)