Library tutorials & articles
Simple DOM Access
- Introduction
- JavaScript functions
- Client Code
- Simple SDA Access
- Live Example
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;
Related articles
Related discussion
-
vb6 generated help file not working in Window 7 and Vista
by Thushan Fernando (1 replies)
-
(Very urgent)how to assign the value of the variable in javascript function into php variable
by mazhar_qayyum (3 replies)
-
Postback data problem
by royal ludhiana (1 replies)
-
Problem Regarding .NET coding (Using Java Script,Ajax) Plz Plz Help me.
by boulou (0 replies)
-
Treeview with Ajax and xml
by lgmperera (0 replies)
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...
This thread is for discussions of Simple DOM Access.