Author: David Nishimoto davepamn@relia.net
Simple Dom Access Source Code
sda.js source codeIntroduction
Usage: The Simple DOM Access (SDA) provide a very abstract perspective for interacting with an xml structure. The xml structure only contains elements named "node". Creating an xml structure with only node elements provides for easy search capabilities when using the selectSingleNode and selectNodes methods of the Document Object Model (DOM).The secret to identifing relationships in the xml structure is maintained by the "parent_key" and "key" attributes. The "key" attribute must be a unique alpha-numeric phrase. The key is used to find any node element in the xml structure. "node" elements can be added anywhere in the xml structure by using the "parent_key" to find the parent node and appendChild the new node into the structure. Likewise, removing a node requires the "parent_key" and the "key". Attributes are associated with a particular "node" element. A "node" is retrieved using the "key" attribute filter match. An attribute is set to the "node" attribute and its value assigned. Likewise, attributes can be remove from an element. The javascript package to access methods in the Simple DOM Access is call "sda". function addNode(sParent_Key,sKey,sElementName) * Add a node into the xml structure * Rule 1 - sKey must be unique * Rule 2 - sParent_Key must exist function getNode(sKey) * Locates a node in the xml structure by its unique "key" value function traverseTree * Print the contents of the xml structure (nodes and attributes) function next * Returns the "key" of the next node * Rule 1 - Return the descendant node if one exists * Rule 2 - If no descendant node exists return the next sibling * Rule 3 - Otherwise return the parent node's - sibling node function previous * Returns the "key" of the previous node * Rule 1 - Find Sibling - Find the bottom-most child of the sibling * Rule 2 - Return previous sibling * Rule 3 - Return parent node child node. function setAttribute(sKey,sAttribute_Name,sValue) * Associate an attribute name/value to a "node" element function removeAttribute(sKey,sAttribute_Name) * Remove an attribute name from a "node" element function deleteNode(sKey) * Remove a "node" element matching a filtered "key" value function getCurrentNodeKey() * Return the current "key" value for the active node * Used with next and previous methods function setCurrentNodeKey * Set the current key value * Used with next and previous methods Client Code
The developer inserts a javascript tag and a function called load_data() The load_data function accesses the Simple DOM Access package allowing the developer to add nodes and associate attributes to the node element.
Simple DOM 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=""; sXML=sXML+" "; simpleDOMAccessDoc.async = 0; simpleDOMAccessDoc.loadXML(sXML); }"; sXML=sXML+" "; sXML=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("
- ");
do
{
sBuffer=" attributes=(";
for (i=0; i
/*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;