473,790 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

postorder traversal of a DOM tree

Hello all,

I'm trying to read an xml file and create a nested JPopupMenu from
that. The first thing I want to do is to read in the xml file and put
it in a Document using DOM and then do a post-order traversal of the
DOM tree. This will let me start at the bottom of the tree, which will
be the deepest selections in the menu, and add the leaves(JMenuIte m's)
to the parents(JMenu's ) and those parents to the root(JPopupMenu ). Get
the idea? (Better yet, if you have a better idea of getting a nested
JPopupMenu from an xml documnet please let me know !) The problem I'm
having is that the traversal doesnt start at the deepest level first.
Is there any way of ensuring that I can start at the deepest leaf
node? Or ensuring that not starting at the deepest level wont matter
when I'm building the menu (maybe by using hashtables?) ? Thanks!
here is the XTW.java file:
import java.io.*;
import java.util.*;

import javax.xml.parse rs.DocumentBuil der;
import javax.xml.parse rs.DocumentBuil derFactory;

import org.w3c.dom.tra versal.*; //for treewalker
import org.w3c.dom.Doc ument;
import org.w3c.dom.Nod e;
import org.w3c.dom.Nod eList;
import org.w3c.dom.Ele ment;

public class XTW
{
public Document doc = null;
private TreeWalker twi = null;
public XTW(String fileName)
{
convert(fileNam e);
}
public static void main(String [] av)
{
String name = "features-template.xml";

XTW xtw = new XTW(name);

try
{
DocumentTravers al dt = (DocumentTraver sal)xtw.doc;

xtw.twi = dt.createTreeWa lker(xtw.doc,
NodeFilter.SHOW _ALL, null,
false);

xtw.postOrderTr aversal(xtw.twi .firstChild());
}
catch (Exception e)
{
System.err.prin tln("EXCEPTION: " + e.getMessage()) ;

e.printStackTra ce(System.err);
}
}

/////////////

public void nodeInfo(Node node)
{
System.out.prin tln
("\n =======Start NODE INFO =========");
System.out.prin tln("node.getNo deValue():" +
node.getNodeVal ue());

System.out.prin tln("(node.getP arentNode()).ge tNodeName():"
+
(node.getParent Node()).getNode Name());
System.out.prin tln("========== ==END=========\ n");
}

//////////////////
public void postOrderTraver sal(Node node)
{
int nodeType = node.getNodeTyp e();

switch (nodeType)
{
case Node.DOCUMENT_N ODE:
postOrderTraver sal(((Document) node).getDocume ntElement());

case Node.ELEMENT_NO DE:
NodeList children = node.getChildNo des();

if (children != null)
{
for (int i = 0; i < children.getLen gth(); i++)
postOrderTraver sal(children.it em(i));

nodeInfo(node.g etFirstChild()) ;
}
}
}
//////////////////////

/** Convert the file */

protected void convert(String fileName)
{
Reader is;

try
{

// Make the document a URL so relative DTD works.

String uri = "file:" + new
File(fileName). getAbsolutePath ();

DocumentBuilder Factory
factory = DocumentBuilder Factory.newInst ance();

DocumentBuilder builder = factory.newDocu mentBuilder();

doc = builder.parse(u ri);

//doRecursive(doc );

}
catch (Exception ex)
{
System.err.prin tln("Exception is convert method");

System.err.prin tln(ex.getClass ());

System.err.prin tln(ex.getMessa ge());
}
}
} //end class

-------------and here is the xml file it reads:features-template.xml
<menu>
<topmenu> feature ="n"
<submenu> feature="number "
<submenu> feature="sing"
<menuitem> feature="sing_t ext" </menuitem>
<menuitem> feature="sing_t ext_two" </menuitem>
</submenu>
<submenu> feature="pl"
<submenu> feature="sub_pl "
<menuitem> feature="sub_pl _text"</menuitem>
</submenu>
</submenu>
</submenu>
</topmenu>
</menu>
----------------------------------------here's the output:
///the deepest would be:
//<menuitem> feature="sub_pl _text"</menuitem>

=======Start NODE INFO =========
node.getNodeVal ue(): feature="sing_t ext"
(node.getParent Node()).getNode Name():menuitem
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="sing_t ext_two"
(node.getParent Node()).getNode Name():menuitem
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="sing"

(node.getParent Node()).getNode Name():submenu
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="sub_pl _text"
(node.getParent Node()).getNode Name():menuitem
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="sub_pl "

(node.getParent Node()).getNode Name():submenu
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="pl"

(node.getParent Node()).getNode Name():submenu
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature="number "

(node.getParent Node()).getNode Name():submenu
============END =========
=======Start NODE INFO =========
node.getNodeVal ue(): feature ="n"

(node.getParent Node()).getNode Name():topmenu
============END =========
=======Start NODE INFO =========
node.getNodeVal ue():

(node.getParent Node()).getNode Name():menu
============END =========
Jul 17 '05 #1
2 4608
ravi mannan wrote:
Hello all,

I'm trying to read an xml file and create a nested JPopupMenu from
that. The first thing I want to do is to read in the xml file and put
it in a Document using DOM and then do a post-order traversal of the
DOM tree. This will let me start at the bottom of the tree, which will
be the deepest selections in the menu, and add the leaves(JMenuIte m's)
to the parents(JMenu's ) and those parents to the root(JPopupMenu ). Get
the idea? (Better yet, if you have a better idea of getting a nested
JPopupMenu from an xml documnet please let me know !) The problem I'm
having is that the traversal doesnt start at the deepest level first.
Is there any way of ensuring that I can start at the deepest leaf
node? Or ensuring that not starting at the deepest level wont matter
when I'm building the menu (maybe by using hashtables?) ? Thanks!


Personally I would approach the traversal this way (pseudocode):

function visit(JMenu parent, Node current)
{
if (current.hasChi ldren())
{
JMenu curMenu = new JMenu(current);
parent.add(curM enu);
for (child in current.getChil dren())
{
visit(curMenu, child);
}
}
else
{
parent.add(new JMenuItem(curre nt));
}
}

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
Thanks for the help, but i just simply made the xml so that the most
nested portions appear above the least nested. thx again

Raymond DeCampo <rd******@spam. twcny.spam.rr.s pam.com.spam> wrote in message news:<HB******* *************@t wister.nyroc.rr .com>...
ravi mannan wrote:
Hello all,

I'm trying to read an xml file and create a nested JPopupMenu from
that. The first thing I want to do is to read in the xml file and put
it in a Document using DOM and then do a post-order traversal of the
DOM tree. This will let me start at the bottom of the tree, which will
be the deepest selections in the menu, and add the leaves(JMenuIte m's)
to the parents(JMenu's ) and those parents to the root(JPopupMenu ). Get
the idea? (Better yet, if you have a better idea of getting a nested
JPopupMenu from an xml documnet please let me know !) The problem I'm
having is that the traversal doesnt start at the deepest level first.
Is there any way of ensuring that I can start at the deepest leaf
node? Or ensuring that not starting at the deepest level wont matter
when I'm building the menu (maybe by using hashtables?) ? Thanks!


Personally I would approach the traversal this way (pseudocode):

function visit(JMenu parent, Node current)
{
if (current.hasChi ldren())
{
JMenu curMenu = new JMenu(current);
parent.add(curM enu);
for (child in current.getChil dren())
{
visit(curMenu, child);
}
}
else
{
parent.add(new JMenuItem(curre nt));
}
}

Ray

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2807
by: FrankEBailey | last post by:
I've been reading up on Modified Preorder Tree Traversal and it's definitely ideal for the kind of tree structures I need to model for my company's multi-level sales hierarchy. I've implemented the database side already in SQL Server 2000 and can retrieve all child nodes based on the left and right IDs of the current node. My problem is displaying the tree so that correct indentation can be used to show the relationships between the...
2
3229
by: mdeni | last post by:
I am sorry if this was allready asked or discussed, please redirect me. I have to make the program of postorder traversal of the binary search tree NOT using recursion. I have found many solutinos using recursion but I can't figure 1 without recursion. Can you help me and tell me the code? Please. With recursion: void postorder(struct node *bt) { if(bt!=NULL) { postorder(bt->left); postorder(bt->right);
2
3799
by: xandra | last post by:
i have 2 question in breadth-first traversal.your help will be appreciated. 1. what is the purpose of the queue in breath-first traversal? 2 suppose you had a function call displayAtDepthN which when given a tree and depth would display only the nodes at that depth. explain how this could be used to give a breadth-first traversal of the tree,and why it would not be as efficient as one using a queue.
6
4068
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
9
15254
by: nishit.gupta | last post by:
Can somebody please help me for algorithm for postorder bst traversal without recursion. It should use stack. i am not able to implement it. thnx
1
2661
by: katdaniel16 | last post by:
how to construct a binary tree from inorder and postorder using the c programming language.. pseudocode will do.. thanks
6
20019
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree are printed on a separate line. my codes are below,( for printing inorder, preorder and post order) I have no Idea how I can print them in , level-order traversal I think I should use a Queue, but how? do you have any code that can help me? would...
2
3822
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
2662
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.