Technically, there are three main ways to read and manage XML text in an ASP environment:
Creates the MSXML object and loads the XML document into the DOM;
Use server-side embedding (Server-side Include,ssi);
Use FileSystemObject to access XML documents just as you would access other text files;
The fourth approach is to create a built-in data island on the client, and then explain the content later.
first, using the DOM
In order to use DOM in ASP code, you need to create an instance of a Microsoft XML parser that is instantiated like any other COM component, Xu to add a few lines of standard code at the beginning of the page. The code creates a parser instance, loads the XML document into the DOM, and sets the root element, which is the document element, as 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, you need to perform step fourth, which is to set the Validateonparse property to True, which ensures that the loaded document is a valid XML document. This avoids all the trouble that comes with the following:
Instatiate the XML Processor
Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
The Processos 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 is also present before loading. It requires that the file be loaded synchronously:
Objxml.async = False
This says when loading and verifying a fairly large file takes some time. Another alternative is to ignore this step and allow asynchronous loading, which is the default, and once these initialization steps are completed, the XML document is loaded and prepared for processing. All the important functions of the DOM are configurable.
Of course, just like any COM object, after use, remember that you must destroy it:
Set Objxml = Nothing
second, server-side embedding
Server-side embedding can be used to insert XML document code into an ASP page.
example 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)
Set Source = Nothing
%>
</BODY>
</HTML>
Contacts.xml
<?xml version= "1.0"?>
<CONTACT_INFO>
<CONTACT>
<NAME>JOHN</NAME>
<PHONE>111-1111-111</PHONE>
</CONTACT>
<CONTACT>
<NAME>SMITH</NAME>
<PHONE>222-2222-222</PHONE>
</CONTACT>
<CONTACT>
<NAME>MIKE</NAME>
<PHONE>333-3333-333</PHONE>
</CONTACT>
</CONTACT_INFO>
XML data formatted by XSL
Stylecontact.asp
<HTML>
<BODY>
<%
SourceFile = Server.MapPath ("Contact.xml")
Stylefile = Server.MapPath ("contact.xsl")
Set Source = Server.CreateObject ("Microsoft.XMLDOM")
Source.async = False
Source.load (sourcefile)
Set style = Server.CreateObject ("Microsoft.XMLDOM")
Style.async = False
Style.load (Stylefile)
Response.Write (Source.transformnode (style))
%>
</BODY>
</HTML>
Contact.xml
<?xml version= "1.0"?>
<?xml-stylesheet type= "text/xsl" href= "contact.xsl"?>
<CONTACT_INFO>
<CONTACT>
<name>zhou. Zf</name>
<PHONE>11111111111</PHONE>
</CONTACT>
<CONTACT>
<NAME>LISTEN</NAME>
<PHONE>22222222222</PHONE>
</CONTACT>
<CONTACT>
<NAME>BUBU</NAME>
<PHONE>33333333333</PHONE>
</CONTACT>
</CONTACT_INFO>
Contact.xsl
<?xml version= "1.0"?>
<xsl:template xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<HTML>
<BODY>
<xsl:for-each select= "Contact_info/contact" >
<DIV>
<xsl:value-of select= "NAME"/>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
Current 1/2 page
12 Next read the full text