MSXML Tutorial - The LoadNewNode() Function
(Page 5 of 5 )
The InsertFirst(), InsertLast(), InsertBefore(), and InsertAfter() functions must retrieve information about the new CD from the text area. This is done by calling the LoadNewNode() method. The LoadNewNode() method loads information about the new CD from the text area into the DOM parser and then returns a reference to the root node of the information about the new CD to one of the four functions that called it. Here’s the LoadNewNode() method:
function LoadNewNode()
{
var xmlNewNode = document.all("newnode").value;
var objNewNode = new ActiveXObject("MSXML2.DOMDocument.4.0");
objNewNode.async = false;
objNewNode.loadXML(xmlNewNode);
if (objNewNode.parseError.errorCode != 0)
{
alert("Error loading new node: " + objNewNode.parseError.reason);
return null;
}
else
{
return objNewNode.documentElement;
}
}
The first line retrieves text from the text area on the HTML form.
The second line creates a new DOMDocument object that contains information about the new CD.
The third line sets the value for the async property to false so that the entire document loads before returning control to the calling point.
The fourth line calls the loadXML() method of the DOMDocument object. The loadXML() method works similarly to the load() method called within the LoadDocument() function except the loadXML() method is used when the argument is a string. In this case, you’re passing the actual XML document as an argument instead of passing a URL that points to the document.
The fifth line checks if an error occurred when loading information about the new CD. If there is an error, then an error message is displayed. If there isn’t an error, then the value of the documentElement of the DOMDocument is returned to the statement that called the LoadNewNode() method. The documentElement is the root element of the document, which is a reference to the <cd> element and all its child elements.
Please check back next week for the second part of this article.