xml spy
Previous  Top  Next
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;
}

Previous  Top  Next

⌐ 2002 Altova