02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 MS<strong>XML</strong><br />

File name: <br />

<br />

The first line creates the input field, and the second line creates the button.<br />

Look at the opening tag on the HTML document and you’ll see that you<br />

tell the browser to call the LoadDocument() JavaScript function each time that the<br />

HTML page is loaded into the browser. This causes the browser to load the default<br />

file and display it in the text area of the web page.<br />

<br />

Notice that the onclick attribute of the input button also calls the LoadDocument()<br />

function when the button is selected. This time the LoadDocument() function loads<br />

the file that’s named in the File name: input box, which is then displayed in the text<br />

area of the web page replacing the current file. You may want to use this button<br />

periodically to refresh the <strong>XML</strong> document to its original state.<br />

The LoadDocument() Function<br />

A function is a piece of code that contains one or more lines of code that execute<br />

only if the function is called by another part of the application. Each function has a<br />

unique name that’s used to call it. A function is defined before it’s called. You’ll<br />

notice that the LoadDocument() function is defined at the beginning of the HTML<br />

file.<br />

LoadDocument() is a JavaScript function that loads a document. Here’s what it<br />

looks like:<br />

var obj<strong>XML</strong>;<br />

function LoadDocument()<br />

{<br />

var inputfile = document.all("inputfile").value;<br />

obj<strong>XML</strong> = new ActiveXObject("MS<strong>XML</strong>2.DOMDocument.4.0");<br />

obj<strong>XML</strong>.async = false;<br />

obj<strong>XML</strong>.load(inputfile);<br />

if (obj<strong>XML</strong>.parseError.errorCode != 0)<br />

{<br />

alert("Error loading input file: " + obj<strong>XML</strong>.parseError.reason);<br />

return;<br />

}<br />

document.all("xmldoc").value = obj<strong>XML</strong>.xml;<br />

}<br />

There are two components shown in this example. The first is obj<strong>XML</strong>. This is<br />

a variable. Think of a variable as a placeholder for a real value. The obj<strong>XML</strong> is a<br />

global variable defined outside the function definition, which means that it can be<br />

159

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!