The DOM and XMLData
The XMLData interface gives you full access to the XML structure behind the current document with less methods than DOM and is much simpler. The XMLData interface is a minimalist approach to reading and modifying existing, or newly created XML data. You might however, want to use a DOM tree because you can access one from an external source or you just prefer the MSXML DOM implementation.
The ProcessDOMNode() and ProcessXMLDataNode() functions provided below convert any segments of an XML structure between XMLData and DOM.
To use the ProcessDOMNode() function:
•pass the root element of the DOM segment you want to convert in objNode and
•pass the plugin object with the CreateChild() method in objCreator
To use the ProcessXMLDataNode() function:
•pass the root element of the XMLData segment in objXMLData and
•pass the DOMDocument object created with MSXML in xmlDoc
////////////////////////////////////////////////////////////////
// DOM To XMLData conversion
Function ProcessDOMNode(objNode,objCreator)
{
var objRoot;
objRoot = CreateXMLDataFromDOMNode(objNode,objCreator);
If(objRoot) {
If((objNode.nodeValue != Null) && (objNode.nodeValue.length > 0))
objRoot.TextValue = objNode.nodeValue;
// add attributes
If(objNode.attributes) {
var Attribute;
var oNodeList = objNode.attributes;
For(var i = 0;i < oNodeList.length; i++) {
Attribute = oNodeList.item(i);
var newNode;
newNode = ProcessDOMNode(Attribute,objCreator);
objRoot.AppendChild(newNode);
}
}
If(objNode.hasChildNodes) {
try {
// add children
var Item;
oNodeList = objNode.childNodes;
For(var i = 0;i < oNodeList.length; i++) {
Item = oNodeList.item(i);
var newNode;
newNode = ProcessDOMNode(Item,objCreator);
objRoot.AppendChild(newNode);
}
}
catch(err) {
}
}
}
Return objRoot;
}
Function CreateXMLDataFromDOMNode(objNode,objCreator)
{
var bSetName = True;
var bSetValue = True;
var nKind = 4;
switch(objNode.nodeType) {
Case 2:nKind = 5;break;
Case 3:nKind = 6;bSetName = False;break;
Case 4:nKind = 7;bSetName = False;break;
Case 8:nKind = 8;bSetName = False;break;
Case 7:nKind = 9;break;
}
var objNew = Null;
objNew = objCreator.CreateChild(nKind);
If(bSetName)
objNew.Name = objNode.nodeName;
If(bSetValue && (objNode.nodeValue != Null))
objNew.TextValue = objNode.nodeValue;
Return objNew;
}
////////////////////////////////////////////////////////////////
// XMLData To DOM conversion
Function ProcessXMLDataNode(objXMLData,xmlDoc)
{
var objRoot;
objRoot = CreateDOMNodeFromXMLData(objXMLData,xmlDoc);
If(objRoot) {
If(IsTextNodeEnabled(objRoot) && (objXMLData.TextValue.length > 0))
objRoot.appendChild(xmlDoc.createTextNode(objXMLData.TextValue));
If(objXMLData.HasChildren) {
try {
var objChild;
objChild = objXMLData.GetFirstChild(-1);
While(True) {
If(objChild) {
var newNode;
newNode = ProcessXMLDataNode(objChild,xmlDoc);
If(newNode.nodeType == 2) {
// child node is an attribute
objRoot.attributes.setNamedItem(newNode);
}
Else
objRoot.appendChild(newNode);
}
objChild = objXMLData.GetNextChild();
}
}
catch(err) {
}
}
}
Return objRoot;
}
Function CreateDOMNodeFromXMLData(objXMLData,xmlDoc)
{
switch(objXMLData.Kind) {
Case 4:Return xmlDoc.createElement(objXMLData.Name);
Case 5:Return xmlDoc.createAttribute(objXMLData.Name);
Case 6:Return xmlDoc.createTextNode(objXMLData.TextValue);
Case 7:Return xmlDoc.createCDATASection(objXMLData.TextValue);
Case 8:Return xmlDoc.createComment(objXMLData.TextValue);
Case 9:Return xmlDoc.createProcessingInstruction(objXMLData.Name,objXMLData.TextValue);
}
Return xmlDoc.createElement(objXMLData.Name);
}
Function IsTextNodeEnabled(objNode)
{
switch(objNode.nodeType) {
Case 1:
Case 2:
Case 5:
Case 6:
Case 11:Return True;
}
Return False;
}