MSXML Tutorial - Loading a Document
(Page 3 of 5 )
Let’s begin by loading the XML document from the file system into the browser. You accomplish this by entering the name of the XML document into the File name: input field on the HTML form and then selecting the button to refresh the document. These two lines of code within the HTML document create these elements:
<td nowrap>File name: <input type="text"
id="inputfile" value="catalog.xml"></td>
<td><input type="button" onclick="LoadDocument();
" value="Load Document"></td>
The first line creates the input field, and the second line creates the button.
Look at the opening <body> tag on the HTML document and you’ll see that you tell the browser to call the LoadDocument() JavaScript function each time that the HTML page is loaded into the browser. This causes the browser to load the default file and display it in the text area of the web page.
<body onload="LoadDocument();">
Notice that the onclick attribute of the input button also calls the LoadDocument() function when the button is selected. This time the LoadDocument() function loads the file that’s named in the File name: input box, which is then displayed in the text area of the web page replacing the current file. You may want to use this button periodically to refresh the XML document to its original state.
The LoadDocument() Function A function is a piece of code that contains one or more lines of code that execute only if the function is called by another part of the application. Each function has a unique name that’s used to call it. A function is defined before it’s called. You’ll notice that the LoadDocument() function is defined at the beginning of the HTML file.
LoadDocument() is a JavaScript function that loads a document. Here’s what it looks like:
var objXML;
function LoadDocument()
{
var inputfile = document.all("inputfile").value;
objXML = new ActiveXObject("MSXML2.DOMDocument.4.0");
objXML.async = false;
objXML.load(inputfile);
if (objXML.parseError.errorCode != 0)
{
alert("Error loading input file: " + objXML.parseError.reason);
return;
}
document.all("xmldoc").value = objXML.xml;
}
There are two components shown in this example. The first is objXML. This is a variable. Think of a variable as a placeholder for a real value. The objXML is a global variable defined outside the function definition, which means that it can be accessed from anywhere in the application. In contrast, inputfile is a local variable to the LoadDocument() function and is only accessible from within the LoadDocument() function definition.
The second component in this example is the function definition. The function is called LoadDocument(). Code between the French braces ({ }) executes each time another part of the application calls the LoadDocument() function.
The first line in the LoadDocument() function definition accesses the value of the inputfile input box on the HTML form. This is the input box containing the name of the document to load. The value is the name of the document. This file name is assigned to a variable called inputfile.
The second line assigns the objXML variable to an instance of the MSXML DOM Object. This function begins by finding out which file to load, which is then stored to the inputfile. Next, you create an ActiveX object for the DOM parser (see Chapter 7). The version number is supplied because MSXML is designed to coexist with previous versions rather than replace a previous version with the latest version.
TIP Visual Basic, VBScript, C, and C++ access objects using either the ActiveX or COM interface.
The third line determines if the file is being accessed synchronously or asynchronously. The DOMDocument object contains properties and functions (sometimes called methods). One of those properties is called async; it controls how the document is going to behave with your application. By setting the async property to false, you’re saying that you want to wait until the document is loaded before executing the next line of code. If you set the async property to true, then the next line of code executes while the document is still loading.
The fourth line calls the load() method, which is defined in the MSXML API. Notice that the inputfile variable is placed between the parentheses of the load() function. This is referred to as passing a variable. In other words, you’re passing the name of the file that you want the load() function to load. The file name is the URL to the document. You can replace this with any valid URL to load the document.
The fifth line checks for errors to make sure the document loaded properly. This is done by using an if statement. An if statement evaluates a condition. If the condition is true, then code within the French braces is executed; otherwise the code is skipped. In this example, the if statement determines if an error occurred opening the file. If so, then an error message is displayed and the function is terminated. If not, then the application skips to the line following the closed French brace (}). The DOMDocument object has a property called parseError that contains details of any errors that might have occurred. This is an instance of the IXMLDOMParseError object. It checks if the errorCode is not zero, which means an error occurred. If so, then the error message is displayed on the screen.
The sixth line displays the XML document in the text area of the HTML page. Look carefully and you’ll notice that the line references the XML property of the objXML variable. Remember that the objXML variable references the DOMDocument. The XML property of the DOMDocument contains the XML representation of the DOMDocument. Remember, the DOM is a tree type structure. The XML property essentially serializes the DOM back to its familiar markup form.
Next: Adding a New Element >>
More XML Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 10 of XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|