Properties | |
URL | Identifies the source of the current parse tree and is used to initiate a parse of an XML instance.
In both scripting environments will load parse the test.xml file and create a parse tree in memory.
|
root | This property holds the root element of the parse tree. To start navigating the tree, you must first retrieve this element. JScript: VBScript:
|
Methods | |
createElement | This method creates a new element object of a specified type. This might seem a bit confusing since Microsoft deemed it necessary to use the word element as an object type in addition to its meaning to XML. The value
returned from this method is a Microsoft object model Element object. It does not automatically add the element to the document; this is done through the Element object. Element types for the Microsoft object model are defined as follows: 0 - ELEMENT 1 - TEXT 2 - COMMENT 3 - DOCUMENT 4 - DTD To create a new element called User: JScript: VBScript:
Similarly, to add a comment:JScript: Notice that whether you create an actual XML element or a comment, the return type is an Element object.
|
Figure 3 Element Object Properties and Methods
Properties | |
type | This is a numerical entity that represents the element type as described for createElement. |
tagName | This is the name of an element returned as a string. The element names you are most interested in are those with a type of 0 (XML elements). |
text | This property contains the textual content between a start and end tag. If this content contains additional elements, they are stripped out of the returned string. For comments, text contains the string associated with the comment if one exists. |
parent | This property holds a pointer to the immediate ancestor in the parse tree. This is very useful when navigating the tree because, given any element in the tree, it is possible to walk to the top and start over again. The parent property contains an Element object. |
children | The collection of elements contained within the start and end tag for a particular element. |
Methods | |
addChild | Adds a new child element to the current element being referenced. |
removeChild | Removes a child element that is associated with the current referenced element. Consequently, this also removes all child elements of the element being removed. |
getAttribute | Returns the attribute of a given name from the currently referenced element. |
setAttribute | Adds or sets an attribute on the currently referenced element. |
removeAttribute | Removes an attribute from the currently referenced element. |
Figure 4 Generating HTML
<%@ Language=VBScript %>
<HTML><HEAD><META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
on error resume next
' If the user has already logged in there will be a hidden
' input field with the user id set; otherwise we need to send
' the user the log on form
id = Request.Form("userid")if (id = "") then
LogonPage
Else
'If the user has provided us with a user id, we need to determine
'the next step in the process. If an XML file does not exist, we need to
'create one, otherwise we want to process it.
set tree = Server.CreateObject("msxml")
Err.Clear
tree.URL = "file:///C:\"&id&".xml"
set root=tree.root
if (Err.number = 0) then
Response.Write("Hello "&root.children.item("USERID").text&"<br>")
Response.Write("You are on page:
"&root.children.item("PAGE",0).text&"<br>")
Response.Write("To view the XML source open file: C:\"&id&
".xml on the Web server")
else
%>
<FORM ACTION="http://gemhaddar/mind_1/persist.asp" METHOD="POST"
id=form1 name=form1>
<INPUT type="hidden" value="<%Response.Write(id)%>" id=userid
name=userid>
<INPUT type="submit" value="Now Click Here" id=submit1 name=submit1>
</FORM>
<%
writeXML
end if
end if
%>
<%
sub LogonPage%>
<FORM ACTION="http://gemhaddar/mind_1/persist.asp" METHOD="POST">
Username: <INPUT type="text" id=userid name=userid>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</FORM>
<%End sub
Sub writeXML
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim filename
Set fs = CreateObject("Scripting.FileSystemObject")
filename = "C:\"&id&".xml"
Err.Clear
Set f = fs.CreateTextFile(filename, true)
if (Err.number = 0)then
f.WriteLine("<?xml version="&chr(34)&"1.0"&chr(34)&"?>")
f.WriteLine("<USER>")
f.WriteLine(" <USERID>"&id&"</USERID>")
f.WriteLine(" <PAGE>PAGE 1</PAGE>")
f.WriteLine("</USER>")
f.Close
end if
End Sub
%>
</BODY>
</HTML>
Figure 5 MyInfo
<%@ Language=VBScript %><HTML><HEAD><META NAME="GENERATOR"
Content="Microsoft Visual Studio 6.0"></HEAD><BODY>
<%Set MyInfoObject = Server.CreateObject("MSWC.MyInfo")
MyInfoObject.PersonalName = "JP Morgenthal"
MyInfoObject.CompanyName = "NC.Focus"
MyInfoObject.CompanyStreet = "11 Starfire Court"
MyInfoObject.CompanyCity = "Hewlett"
MyInfoObject.CompanyState = "NY"
MyInfoObject.CompanyZIP = "11557"
MyInfoObject.CompanyPhone = "(516) 792-0997"
%>
Now terminate your Web server process and open file MyInfo.xml
</BODY>
</HTML>
Figure 6 Code for Server
<%@ Language=JScript %><%var xml = Request.QueryString("XML");
writeXML(xml);
var document = new ActiveXObject("msxml");
document.URL="file:///C:\\tmp.xml";
var root = document.root;
var name = root.children.item("NAME").text;
if (name == "reverse") {
var param = root.children.item("PARAMETER").text;
var str2 = new String(param);
var ostr="", i, j;
for (i=(str2.length-1); i>=0; i--) {
ostr = ostr + str2.charAt(i);
}
var elem = root.children.item("PARAMETER").children.item(0);
elem.text = ostr;
DumpTree(root);
} else {
Response.Write(xml);
}
function DumpTree(root){
var i;
if (root.type == 1) {
Response.Write(root.text);
} else {
Response.Write("<"+root.tagName+">");
if (root.children != null) {
for (i=0; i<root.children.length; i++)
DumpTree(root.children.item(i));
}
Response.Write("</"+root.tagName+">");
}
}
function writeXML (xml) {
var ForReading = 1, ForWriting = 2, ForAppending = 8
var fs = new ActiveXObject("Scripting.FileSystemObject")
var f = fs.CreateTextFile("C:\\tmp.xml", true)
if (f != null) {
f.Write(xml)
f.Close();
}
}
%>
Figure 7 Code for Client
<html>
<head>
<title>XML Remote Procedure Call Example</title>
<style>
td {font-size:80%;}
xmp {font-size:120%; background:yellow}
</style>
</head>
<SCRIPT language=Javascript>
function ShowXML(doc)
{
var w = window.open("","","resizable,scrollbars,width=640,height=480");
w.title = "XML Data";
w.document.open();
w.document.write("<STYLE>");
w.document.write(" .xml {font-size:12pt;font-family:Arial}");
w.document.write(" .tag {color:red; font-weight:bold; }");
w.document.write(" .attribute {color:blue; font-weight:bold; }");
w.document.write(" .attrvalue {color:darkorchid; font-weight:bold; }");
w.document.write(" .comment {color:green; font-weight:bold; }");
w.document.write("</STYLE>");
var xml = DumpTree(doc.root,0);
w.document.write(xml);
w.document.close();
}
function DumpTree(node,i)
{
var result = "<DL class=xml><DD>";
if (node.type == 2) {
result += "<span class=comment><!--"
+ node.text + "--></span>";
result += "</DD></DL>"
return result;
}
var num = node.numElements();
var empty = "";
if (num == 0) empty = "/";
var tagname = node.tagName;
result += "<span class=tag><" + tagname + "</span>";
var attrs = node.attributes;
while (attrs != null && attrs.hasMoreElements()) {
var a = attrs.nextElement();
result += " <span class=attribute>" + a.getName() +
"</span>=<span class=attrvalue>\"" + a.getValue() + "\"</span>";
}
result += "<span class=tag>" + empty + "></span>";
if (num > 0) {
if (isMixed(node) > 0) {
result += node.text;
} else {
var j;
for (j = 0; j < num; j++) {
result += "\n";
var child = node.getChild(j);
if (child.type != 12)
result += DumpTree(child,i+1);
}
}
result += "<span class=tag></" + tagname + "></span>\n";
}
result += "</DD></DL>"
return result;
}
function isMixed(node)
{
var num = node.numElements();
var j;
for (j = 0; j < num; j++) {
var child = node.getChild(j);
var type = child.type;
if (type == 1 || type == 6 || type == 11)
return 1;
}
return 0;
}
</SCRIPT>
<BODY TOPMARGIN=0 LEFTMARGIN=40 BGCOLOR="#ffffff" LINK="#000066" VLINK="#666666" TEXT="#000000">
<FONT FACE="verdana,arial,helvetica" SIZE=2>
<P>
<APPLET code=com.ms.xml.dso.XMLDSO.class height=45 id=xmldso width=100%
MAYSCRIPT = true>
<PARAM NAME="url" VALUE="http://gemhaddar/mind_1/rpc.asp?xml=
<FUNCTION><NAME>reverse</NAME><PARAMETER>JP Morgenthal</PARAMETER>
</FUNCTION>">
</APPLET>
Here is the XML Function Call:
<FONT size="2"><PRE> <FONT face="" size=3><FUNCTION>
<NAME>reverse</NAME>
<PARAMETER>JP Morgenthal</PARAMETER>
</FUNCTION></FONT>
</PRE></FONT>
<input type=button value="Show Results"
onclick='ShowXML(xmldso.getDocument());' id=button1 name=button1>
<P></P></FONT>The APPLET tag that invokes the XML DSO is shown below:
<xmp id=applet>
<applet code=com.ms.xml.dso.XMLDSO.class
width=100% height=25 id=xmldso MAYSCRIPT=true>
<PARAM NAME="url" VALUE="http://gemhaddar/mind_1/rpc.asp?xml=
<FUNCTION><NAME>reverse</NAME><PARAMETER>JP Morgenthal</PARAMETER>
</FUNCTION>">
</applet>
</xmp>
</BODY>
</html>