CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2008
    Posts
    2

    [RESOLVED] Interesting Question, XML DOM and Java script

    Hello,

    First and foremost please let me know if I am in the wrong forumn for this question and I will repost to a better location


    I am looking for a bit of advice on some code I am writing.


    I have created an explorer tree using input from an XML document. The tree and the links work fine great, thanks to some help from this forum. As you will see in the code and the db file below, this code only works if the xml structure is followed.

    The problem I would like to resolve is I may only place "section nodes(LEAVES)" as children of "manual nodes". I would like to have the ability to place a "section (LEAF)" under a group if I wanted. This will make more sense when you review the DB XML code below

    Try to make this change in the XML doc and you will see the bad results.


    I am not exactly sure how to tackle this problem but I have played with the notion of the hasChildNodes() method, but i can't get it to work quite right.

    The links are arbitrary at this point and are all dummy links.

    As Always help is much Appreciated!!

    The Tree
    Code:
    <html>
     <head>
      <title>Electronic Manuals Tree_1</title>
       <style>
        body
        {
    	font: 10pt Verdana,sans-serif;
    	color: navy;
        }
        .branch
        {
    	cursor: pointer;
    	cursor: hand;
    	display: block;
        }
        .leaf
        {
    	display: none;
    	margin-left: 16px;
        }
        a
        {
    	text-decoration: none;
        }
        a:hover
        {
    	text-decoration: underline;
        }
       </style>
        <script language="JavaScript">
    	// Class variables For Images
    	var openImg = new Image();
    	openImg.src = "images/open.gif";
    	var closedImg = new Image();
    	closedImg.src = "closed.gif";
    
    	// Constructor Functions
    	function tree()
    	{
    		this.branches = new Array();
    		this.add = addBranch;
    		this.write = writeTree;
    	}
    
    	function branch(id, text)
    	{
    		this.id = id;
    		this.text = text;
    		this.write = writeBranch;
    		this.add = addLeaf;
    		this.leaves = new Array();
    	}
    	function leaf(text, link)
    	{
    		
    		this.text = text;
    		this.link = link;		
    		this.write = writeLeaf;
    		
    	}
    
    	// Write  and Add Functions 
    
    	function writeTree()
    	{
    		var treeString = '';
    		var numBranches = this.branches.length;
    		for(var i=0;i<numBranches;i++)
    			treeString += this.branches[i].write();
    		document.write(treeString);
    	}
    
    	function addBranch(branch)
    	{
    		this.branches[this.branches.length] = branch;
    	}
    
    	function writeBranch()
    	{
    		var branchString = '<span class="branch" onClick="showBranch(\'' + this.id + '\')"';
    		branchString += '><img src="closed.gif" id="I' + this.id + '">' + this.text;
    		branchString += '</span>';
    		branchString += '<span class="leaf" id="';
    		branchString += this.id + '">';
    		var numLeaves = this.leaves.length;
    		for(var j=0;j<numLeaves;j++)
    			branchString += this.leaves[j].write();
    		branchString += '</span>';
    		return branchString;
    	}
    
    	function addLeaf(leaf)
    	{
    		this.leaves[this.leaves.length] = leaf;
    	}
    
    	function writeLeaf()
    	{
    		var leafString = '<a href="' + this.link + '" target="main">';
    		leafString += '<img src="images/doc.gif" border="0">';		
    		leafString += this.text;
    		leafString +='</a><br>';
    		return leafString;
    	}
    
    	function showBranch(branch)
    	{
    		var objBranch = document.getElementById(branch).style;
    		if(objBranch.display=="block")
    			objBranch.display="none";
    		else
    			objBranch.display="block";
    		swapFolder('I' + branch);
    	}
    
    	function swapFolder(img)
    	{
    		objImg = document.getElementById(img);
    		if(objImg.src.indexOf('closed.gif')>-1)
    			objImg.src = openImg.src;
    		else
    			objImg.src = closedImg.src;
    	}
     
    	function main()
    	{
    		
    		// Loads the XMl doc
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            	xmlDoc.load("db.xml");
    		xmlDoc.async=false;
    		if(!xmlDoc.documentElement)alert("Invalid XML Document");
    
    		
    		// Variable declarations
    		// nodelists
    		var x = xmlDoc.getElementsByTagName('Manual');
    		var g = xmlDoc.getElementsByTagName('Group');
    		//var s = xmlDoc.getElementsByTagName('SubManual');
    		var m = xmlDoc.getElementsByTagName('section');
    
    		// initial branch and tree
    		var myTree = new tree();
    		var branch1 = new branch('branch1','Navigation Folder');
    
    		// branch and leaf id start points
    		var id = 0;
    		var m_id = 100;
    		var s_id = 500;
    		var sec_id = 750;
    
    		// initial node values
    		var nodeGroup = g[0].firstChild;
    		var nodeManual = x[0].firstChild;
    		//var nodeSubManual = s[0].firstChild;
    		var nodeSection = m[0].firstChild; 
    
    		// counters
    		var counter = 1;
    		var manualCounter = 0;
    		var groupCounter = 0;
    		var subManualCounter = 0;
    		var sectionCounter = 0;
    		var ml_loop = 0;
    		var sml_loop = 0;
    		var sec_loop = 0;
    		var counter_nm = 0;
    		var counter_nsm = 0;
    		var counter_nsec = 0;
    		
    		// length holders
    		var gl = g.length;
    		var ml = x.length -1;
    		//var sl = s.length -1;
    		var cl = m.length -1;
    		
    		var bool = true;
    		var nodeHolder;
    
    	
    		
    
    		// Group Loop
    		for (r=0;r<gl;r++)
    		{ 	
    				
    			branch2 = new branch(id,nodeGroup.data);
    			nodeGroup = g[counter].firstChild;			
    			if (counter < gl-1) counter ++;
    			ml_loop = g[manualCounter].childNodes.length-1;
    			
    			// Manual Loop
    			for(j=0;j<ml_loop;j++)
    			{
    				branch3 = new branch(m_id,nodeManual.data);
    				branch2.add(branch3);
    				m_id++;
    				if (counter_nm < ml)counter_nm ++;
    				nodeManual = x[counter_nm].firstChild;
    
    					//leaf loop
    					sec_loop = x[sectionCounter].childNodes.length-1;
    					if(sec_loop > 0)
    					{
    						for(p=0;p<sec_loop;p++)
    						{
    							branch3.add( new leaf(nodeSection.data, "http://www.yahoo.com"));
    							if (counter_nsec < cl)counter_nsec ++;
    							nodeSection = m[counter_nsec].firstChild;
    							 
    						}
    					}			
    					if(sectionCounter < ml) sectionCounter ++;
    
    								
    			}
    			branch1.add(branch2);
    			id ++;
    			if(manualCounter < gl)
    				manualCounter ++;		
    			groupCounter ++;													
    	    	}
    		myTree.add(branch1);								
    		myTree.write();			 
    					
    	}
    
     
         </script>
       </head>
         <body bgColor=Red>
          <script language="JavaScript">
    	main();	
         </script>	
        </body>
    </html>
    The XML DB doc:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Manuals>
    	<Group>Currency Lists
    			<Manual>Currency000
    				<section>Section000
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>		
    			</Manual>
    			<Manual>Currency111
    				<section>Section111
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section222
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>		
    			</Manual>
    			<Manual>Currency222	
    			</Manual>
    	</Group>
    	<Group>General
    		<Manual>Aircraft Loading
    		</Manual>
    		<Manual>Compliance Statement
    				<section>Section_119
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section_121
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section_830
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section_91
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section_43
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section_39
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>				
    		</Manual>
    		<Manual>Deice
    		</Manual>
    		<Manual>Operation Specification
    		</Manual>
    		<Manual>Technical Publications
    		</Manual>
    	</Group>
    	<Group>Flight Operations
    		<Manual>Flight Control
    		</Manual>
    		<Manual>General Operations
    		</Manual>
    		<Manual>Training
    		</Manual>
    		<Manual>B737 Manuals
    		</Manual>
    	</Group>
    	<Group>Maintenance
    		<Manual>DFDR System Maintenance Program
    		</Manual>
    		<Manual>General Maintenance
    		</Manual>
    		<Manual>RVSM System Maintenance Program
    		</Manual>
    		<Manual>Stockroom Program
    		</Manual>
    		<Manual>B 737 Manuals (CAMP)
    		</Manual>
    		<Manual>B 737 Manuals (CAMP Master Workcards)
    		</Manual>
    		<Manual>B 737 Manuals (Maintenance)
    		</Manual>
    		<Manual>Minimum Equipment List
    		</Manual>
    		<Manual>CMM
    		</Manual>
    	</Group>
    	<Group>Saftey
    		<Manual>Emergency Response Plan
    		</Manual>
    		<Manual>Internal Evaluation Program
    		</Manual>
    		<Manual>Safety
    		</Manual>
    	</Group>
    	<Group>CheckLists
    	</Group>
    	<Group>Handbooks
    		<Manual>Employee
    		</Manual>
    		<Manual>Emergency Preparedness
    		</Manual>
    		<Manual>Hazmat
    		</Manual>
    	</Group>
    	<Group>Links
    		<Manual>FAA
    		</Manual>
    		<Manual>SAI's
    		</Manual>
    	</Group>
    	<Group>Forms
    	</Group>	
    </Manuals>
    XML I am trying to implement:
    Code:
    	<Group>Currency Lists
    
    				<section>TEST
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    
    
    			<Manual>Currency000
    				<section>Section000
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>		
    			</Manual>
    			<Manual>Currency111
    				<section>Section111
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>
    				<section>Section222
    					<link>B737_MEL/Section_00.pdf</link>
    				</section>		
    			</Manual>
    			<Manual>Currency222	
    			</Manual>
    	</Group>

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Interesting Question, XML DOM and Java script

    First and foremost please let me know if I am in the wrong forumn for this question and I will repost to a better location
    Java script is not Java, try posting the client side forum

  3. #3
    Join Date
    Apr 2007
    Posts
    442

    Re: Interesting Question, XML DOM and Java script

    This is a Java forum, not JavaScript forum. Would be good, if you posted that to a proper arena, you would be more likely to find people having something constructive to say about it.

    good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured