Technically, there are three main methods to read and manage XML text in the ASP environment:
Create an MSXML object and load the XML document to the Dom;
Server-side include (SSI );
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, which will be explained later.
I. Use dom In order Code To use Dom, you need to create an instance of Microsoft XML analyzer, Which is instantiated like any other COM component, and add several lines of standard code at the beginning of the page. These codes create an analyzer instance, load the XML document to the Dom, and set the root element (that is, 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.doc umentelement
Before the XML document is loaded, you also need to perform Step 4, that is, set the validateonparse attribute to true, which ensures that the loaded document is a valid XML document. This avoids the following troubles:
Instatiate the XML Processor
Set objxml = server. Createobject ("Microsoft. xmldom ")
The processos shocould validate the document
Objxml. validateonparse = true
Load the XML document
Objxml. Load (server. mappath ("mydata. xml ")
Set the document Element
Set objrootelement = objxml.doc umentelement
Finally, there is an optional step, which also appears before loading. It requires synchronous file loading:
Objxml. async = false
It takes some time to load and verify a large file. Another alternative solution is to ignore this step and allow non-synchronous loading. This is the default situation. Once these initialization steps are completed, the XML document will be loaded and ready for processing. All important functions of Dom are configurable.
Of course, just like any COM Object, remember to destroy it after use:
Set objxml = nothing
Ii. server-side embedding Server-side embedding can be used to insert XML document code into ASP pages.
Iii. Example of XML Processing Using ASP code <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.doc umentelement
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>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.