Other examples:
1. WEB link
<HTML>
<BODY>
<%
Dim objXML, objRootElement, strLinkName, strLinkURL, strLinkTarget, sourceFile, i
sourceFile = ("APPL_PHYSICAL_PATH") & "xml\"
set objXML = ("")
= True
= false
(sourceFile)
set objRootElement =
for i = 0 to -1
strLinkName = (i).text
strLinkURL = (i).getAttribute("url")
strLinkTarget = (i).getAttribute("target")
("<a href=" & strLinkURL & " target=" & strLinkTarget & ">" & strLinkName & "</a><br>")
next
set objXML = nothing
%>
</BODY>
</HTML>
<?xml version="1.0" ?>
<relatedlinks>
<link url="" target="_blank">Front Page 0</link>
<link url="" target="_blank">Front Page 1</link>
<link url="" target="_blank">Front Page 2</link>
<link url="" target="_blank">Front Page 3</link>
</relatedlinks>
2. Generation of XML files
gen_xml.asp
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Generate XML</TITLE>
</HEAD>
<BODY>
<%
Create an empty root node for the document.
Set rootNode = ("")
= "ISO-8859-1"
= "yes"
= "table"
for row = 1 to 10
Set rowNode = ("row","")
for col = 1 to 10
Set colNode = ("column","("&col&","&row&")")
next
next
((""))
Set rootNode = Nothing
%>
</BODY>
</HTML>
3. Append nodes to XML files
<%
If ("REQUEST_METHOD")="POST" Then
call writeXML
Else
call showForm
End If
Sub showForm
%>
<html>
<body>
Please enter the persons contact details
<form action="" method="post">
Name: <input type="text" name="name"> Email: <input type="text" name="email">
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
<%
End Sub
Sub writeXML
find xml file path
Dim strFilename,strXMLFile
strFilename = ""
strXMLFile=(strFilename)
standard "create xml object" code
set oXML = ("")
= False
check file exists
bitFileExists = (strXMLFile)
If bitFileExists Then
tmpChildnodes = - 1
intCurrentRecord = tmpChildnodes + 1
Else
create xml
(("xml","version=""1.0"""))
(("contacts"))
intCurrentRecord = 0
End If
create record
set oXMLRec = ("contact")
Create an id attribute
Set oAtt = ("id")
= intCurrentRecord
oAtt
set remaining fields
name
(("name"))
(0).text = ("name")
email address
(("email"))
(1).text = ("email")
date stamp
(("created"))
(2).text = now()
write new record
((True))
save file
strXMLFile
redirect to xml file
(strFilename)
oXML = nothing
End Sub
%>
<?xml version="1.0"?>
<CONTACT_INFO>
<contact >
<name>bubu</name>
<email>zhou813@</email>
<created>2003/04/29 9:26:51</created>
</contact>
<contact >
<name>22222</name>
<email>222222222</email>
<created>2003-5-12 18:15:41</created>
</contact>
</CONTACT_INFO>
4. Message sheet
<%
username=("username")
if username<>"" then
fromwhere =("fromwhere")
homepage =("homepage")
email =("email")
text =(("text"))
Posttime =now()
sourceFile = ""
strSourceFile = (sourceFile)
Set objXML = ("")
blnRet = (strSourceFile)
If blnRet Then
Set objRootlist =
if then
id = +1
else
id=1
end if
Else
(("xml","version=""1.0"""))
set objRootlist = ("NewList")
(objRootlist)
id = 1
End If
brstr=chr(13)&chr(10)&chr(9)
XMLnode=brstr&"<list>"&brstr & _
"<id>"&id&"</id>"&brstr & _
"<username>"&username&"</username>"&brstr & _
"<fromwhere>"&fromwhere&"</fromwhere>"&brstr & _
"<Posttime>"&Posttime&"</Posttime>"&brstr & _
"<homepage>"&homepage&"</homepage>"&brstr & _
"<email>"&email&"</email>"&brstr & _
"<text>"&text&"</text>"&brstr & _
"</list>"&chr(13)
set objXML2=("")
(XMLnode)
set rootNewNode=
(rootNewNode)
(strSourceFile)
set objXML=nothing
set objXML2=nothing
(strSourceFile)
end if
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td { font-size: 9pt}
-->
</style>
</head>
<body bgcolor="#0099CC" text="#000000">
<table width="80%" border="0" cellspacing="1" cellpadding="4" align="center" bgcolor="#FFFFFF">
<form action="" method="post" name="form1">
<tr bgcolor="#000000">
<td colspan="2"><font color="#FFFFFF">New Book</font></td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">Name:</td>
<td width="81%">
<input type="text" name="username">**
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">ComeFrom:</td>
<td width="81%">
<input type="text" name="fromwhere" value="China">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">WebSite:</td>
<td width="81%">
<input type="text" name="homepage" value="http://">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">Email:</td>
<td width="81%">
<input type="text" name="email">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right" valign="top">Context:</td>
<td width="81%">
<textarea name="text" cols="60" rows="10"></textarea>
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right"> </td>
<td width="81%">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</td>
</tr>
</form>
</table>
</body>
</html>
4. Use DOM to generate XML summary
Understand XML Document Object Model
XML DOM contains four main objects: XMLDOMDocument, XMLDOMNode, XMLDOMNodeList, and XMLDOMNamedNodeMap. Like any other object model, each XML DOM object has its own characteristics and methods. In this article, we will mainly introduce XMLDOMDocument and XMLDOMNode objects.
XMLDOMDocument object
The XMLDOMDocument object represents the top-level node in the XML DOM level (please do not be confused with the root node in the XML document). It is the basis for establishing and manipulating XML structures. To create XMLDOMDocument object variables, use the CreateObject command as follows:
Set objXMLdoc = CreateObject("")
Table A: lists some features and methods that we can get after creating an XMLDOMDocument object.
Method Description
CreateAttribute Create new attribute
CreateCDATASection Creates DATA partial nodes
CreateComment Create comment node
CreateElement Create element nodes with the specified name
CreateEntityReference Create entity reference object
CreateNode Create node
CreateTextNode Create text nodes
Load Load an existing XML document
Save XML document
Feature Description
PreserveWhiteSpace Indicates whether to display spaces in XML documents t
ResolveExternals Resolve name domain, DTD and external entity indexes
ValidateOnParse Indicates whether the parser performs legality checks on the document
DocumentElement Returns the root node of the XML document
Set the run option
As you can see in Table A, you can create a single XML node only if the XMLDOMDocument object is created. Before doing this, you can also set the parsing and running conditions of XML documents, which is useful if you plan to directly generate XML documents to the browser.
Run options allow you to set up asynchronous downloads, provide document legitimacy checks and preserve spaces, and parse external indexes. To prevent asynchronous downloads, the asynch feature can be set to False. If you plan to replace spaces with a new line of characters, you can set the PreserveWhiteSpace feature to True. The ResolveExternals feature prevents the resolution engine from resolving external entity indexes, document type definitions, or external name domains.
1. Create nodes
The XMLDOMDocument object provides two methods to create nodes or XMLDOMNodes: CreateElement and CreateNode. Which method you choose depends on how much information you need to provide at the node. When you use the CreateElement method, just provide the node name, such as
Set objXMLroot = ("ORDER_STATUS")
When using the CreateNode method, you specify the node type, node name, and related name domain. (The XML name domain allows you to select the same document to create multiple XML elements with the same name) For example, if you want to use the CreateNode method, you can write the following code:
Set objXMLroot = ("element","ORDER_STATUS","Space1")
Once a node is created, you must add it to the XML document. This requires the AppendChild method. For example, continuing with the above example, you can write:
(objXMLroot)
This statement creates an XML root node.
In addition to the AppendChild method, the XMLDOMNode object also has three other methods to control XML nodes: ReplaceChild, RemoveChild and InsertBefore
2. Assign values to node attributes
Once a node is created, its attributes must also be assigned values, such as independent identifiers, or attribute values. You need to use the SetAttribute method. This method receives two parameters—the attribute name and the attribute value. For example, the following code creates the attribute name SHIPPING_DATASOURCE and the attribute value NORTH_ATLANTIC_SHIPPING:
"SHIPPING_DATASOURCE","NORTH_ATLANTIC_SHIPPING"
At this point, we generate an XML document, and the SetAttributes method creates the node attribute.
3. Add to sub-nodes
To create child nodes, you can use the CreateElement or CreateNode method we introduced earlier and add them to the appropriate parent node. For example, suppose we want to create an XML node called PUBLISHER_DISCLAIMER. To do this, we append this node to the root node of the document, the code is as follows:
Set objXMLChildTestNode = ("element","PUBLISHER_DISCLAIMER","")
(objXMLChildTestNode)
Note: We used the DocumentElement feature of XMLDOMDocument to add new nodes to the root node of the XML document. To add child nodes, you just need to add them to the parent node.
4. Assign value to nodes
Once all nodes are created, you will need to assign values to them. That is the most critical step in XML. To do this, you create a node as usual, and then assign a value to the TEXT attribute of that node.
Previous page12Read the full text