Library tutorials & articles

Simple DOM Access

Simple SDA Access

sda.js

var simpleDOMAccessDoc;
var sCurrentNodeKey;
sCurrentNodeKey="root";
var goBottomNode;

/*
Purpose: Returns the current key value.
Used with the next and previous
methods
*/
function getCurrentNodeKey()
{
return(sCurrentNodeKey);
}
/*



Purpose: Sets the current key
value. Used with the next and
previous methods.
*/


function setCurrentNodeKey(sKey)
{
sCurrentNodeKey=sKey;
}


/* purpose: provides navigation
forward through the xml tree.
Rule 1: check for a descendant
Rule 2: check for a sibling
Rule 3: check for parent nodes sibling
*/
function next()
{
   var oNode;
   var oCurrentNode;
   var oOldNode;
   var sRetText;

   oNode=getNode(sCurrentNodeKey);
   sRetText="";
   
   /*Rule 1 - Check for a Descendant */
   oCurrentNode=oNode.firstChild;

   /*Rule 2 - Check for a Sibling */
   if(oCurrentNode==null)
   {
       oCurrentNode=oNode.nextSibling;
   }    
   /*Rule 3 - Check for Parent Node' Sibling */
   if(oCurrentNode==null)
   {
       oCurrentNode=oNode.parentNode;
       while (oCurrentNode!=null)
       {
           oOldNode=oCurrentNode;

           oCurrentNode=oCurrentNode.nextSibling;
           if (oCurrentNode==null)
           {
               oCurrentNode=oOldNode.parentNode;
           }
           else
           {
               sCurrentNodeKey=oCurrentNode.getAttribute("key");
               break;
           }
       }
   }    

   if (oCurrentNode != null)
   {
       sCurrentNodeKey=oCurrentNode.getAttribute("key");
       sRetText=oCurrentNode.getAttribute("key");
   }
   else
   {
       sRetText=oNode.getAttribute("key");
   }
   return(sRetText);
}



/*
Purpose: provides navigation
back through the xml tree.
Rule 1&2- Find Sibling Node - Find the bottom-most child of the sibling*/
Rule 3 - Find Parent Node
*/
function previous()
{
   var oNode;
   var oCurrentNode;
   var oOldNode;
   var sRetText;

   sRetText="";

   /* Rule 1 - Find Sibling - Find the bottom-most child of the sibling*/
   oNode=getNode(sCurrentNodeKey);
   oCurrentNode=oNode.previousSibling;

   /* Rule 2 - Find Siblings bottom of the tree*/
   if (oCurrentNode!=null)
   {
       goBottomNode=null;
       bottomOfTree(oCurrentNode);
       oCurrentNode=goBottomNode;
   }

   /* Rule 3 - Find Parent*/
   if (oCurrentNode==null)
   {
       oOldNode=oCurrentNode;
       oCurrentNode=oNode.parentNode;
       if (oCurrentNode.getAttribute("key")=="root")
       {
           oCurrentNode=oOldNode;
       }
   }
   if (oCurrentNode != null)
   {
       sCurrentNodeKey=oCurrentNode.getAttribute("key");
       sRetText=oCurrentNode.getAttribute("key");
   }
   else
   {
       sRetText=oNode.getAttribute("key");
   }
   return(sRetText);
}



function simpleDOMAccessClass()
{
var sXML;
simpleDOMAccessDoc=new ActiveXObject("Microsoft.XMLDOM");

sXML="<xml>";
sXML=sXML+"<node parent_key=\'na\' key=\'root\' element_name=\'content\' >";
sXML=sXML+"</node>";
sXML=sXML+"</xml>";

simpleDOMAccessDoc.async = 0;
simpleDOMAccessDoc.loadXML(sXML);
}



/*Purpose: Add a node to the xml tree
1)Search for the parent node
2)Assign attribute values to the new node
3)Insert the new node into the xml tree
*/
function addNode(sParent_Key,sKey,sElementName)
{
   var oNewNode;
   var oParentNode;
   var sCriteria;

   oNewNode=simpleDOMAccessDoc.createElement("node");

   /* 1-Search for Parent Node Match */

   sCriteria="//node[@key $eq$ \ '"+sParent_Key+"\']";

   oParentNode=simpleDOMAccessDoc.selectSingleNode(sCriteria);
   
   /* 2-Insert new Node into Document Object Model (dom) structure*/

   if(oParentNode!=null)
   {
       oNewNode.setAttribute("parent_key",sParent_Key);
       oNewNode.setAttribute("key",sKey);
       oNewNode.setAttribute("element_name",sElementName);
       oParentNode.appendChild(oNewNode);
   }

}


function setAttribute(sKey,sAttribute_Name,sValue)
{
   var oNode;
   
   oNode=getNode(sKey);

   if (oNode != null)
   {
       oNode.setAttribute(sAttribute_Name,sValue);
   }
}



function removeAttribute(sKey,sAttribute_Name)
{
   var oNode;
   
   oNode=getNode(sKey);

   if (oNode != null)
   {
       oNode.removeAttribute(sAttribute_Name);
   }
}



function bottomOfTree(oNode)
{
   goBottomNode=oNode;

   if (oNode.hasChildNodes()>0)
   {
       bottomOfTree(oNode.lastChild);        
   }

}



function traverseTree(oNode)
{
   var oAttribute;
   var sAttribute_Name;
   var sAttribute_Value;
   var sBuffer;
   var i;

   document.write("<dl>");
   do
   {
       sBuffer=" attributes=(";

       for (i=0; i<oNode.attributes.length; i++)
       {
           oAttribute=oNode.attributes[i];
           sAttribute_Name=oAttribute.name;
           sAttribute_Value=oAttribute.value;
           sBuffer=sBuffer+sAttribute_Value+" ";
       }

       sBuffer=sBuffer+")";

       document.write("<dt><ddr>"+oNode.getAttribute("element_name")+" "+sBuffer);

       if (oNode.hasChildNodes()>0)
       {
           traverseTree(oNode.firstChild);        
       }
       oNode=oNode.nextSibling;
   }
   while(oNode != null);
   document.write("</dl>");
}



/*Purpose:locate a specific node in the
xml structure by its "key" value
*/
function getNode(sKey)
{
   var oNode;

   sCriteria="//node[@key $eq$ \ '"+sKey+"\']";

   oNode=simpleDOMAccessDoc.selectSingleNode(sCriteria);

   if(oNode != null)
   {
       return(oNode);
   }
   else
   {
       alert("Node not found");
   }

}



function getSimpleDOMAccessDocument()
{    
   return(simpleDOMAccessDoc);
}


/*Purpose: remove a node from the xml tree
1)Find the node
2)Find the node's parent
3)Invoke the parents removeChild method deleting
the node and its descendants from the xml tree.
*/
function deleteNode(sKey)
{
   var oNode;
   var oParentNode;
   var sParent_Key;

   oNode=getNode(sKey);

   if (oNode != null)
   {
       sParent_Key=oNode.getAttribute("parent_key");
       oParentNode=getNode(sParent_Key);
       if (oParentNode != null)
       {
           oParentNode.removeChild(oNode);
       }
   }
}

var sda=new simpleDOMAccessClass();
sda.addNode=addNode;
sda.getNode=getNode;
sda.traverseTree=traverseTree;
sda.next=next;
sda.previous=previous;
sda.setAttribute=setAttribute;
sda.removeAttribute=removeAttribute;
sda.deleteNode=deleteNode;
sda.getCurrentNodeKey=getCurrentNodeKey;
sda.setCurrentNodeKey=setCurrentNodeKey;

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Simple DOM Access.

Leave a comment

Sign in or Join us (it's free).

David Nishimoto NishiSoft provides Part I of the Information Technology Project collaboration. Sign up and list your IT project tasks, assign task too friends, and get percent complete task. Part will include a wo...
AddThis

Related podcasts

  • Ajax, DWR, and Spring

    Improving the User Experience without the JavaScript hassle: Ajax, DWR, and Spring Buzzwords like AJAX (Asynchronous JavaScript And XML) and XmlHttpRequest are buzzing around Java blogs for months now. The DWR (Direct Web Remoting) project aims to provide easy AJAX for Java. This session will...

Events coming up

  • Nov 20

    Full Frontal JavaScript Conference

    Brighton, United Kingdom

    A one day JavaScript conference held in Brighton, UK whose essence is to discuss JavaScript "with nothing concealed or held back".The conference is being held at one of the world's first cinemas, which first opened in 1910.Speakers include...

We'd love to hear what you think! Submit ideas or give us feedback