473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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(JMenuItem'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.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.traversal.*; //for treewalker
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

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

XTW xtw = new XTW(name);

try
{
DocumentTraversal dt = (DocumentTraversal)xtw.doc;

xtw.twi = dt.createTreeWalker(xtw.doc,
NodeFilter.SHOW_ALL, null,
false);

xtw.postOrderTraversal(xtw.twi.firstChild());
}
catch (Exception e)
{
System.err.println("EXCEPTION:" + e.getMessage());

e.printStackTrace(System.err);
}
}

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

public void nodeInfo(Node node)
{
System.out.println
("\n =======Start NODE INFO =========");
System.out.println("node.getNodeValue():" +
node.getNodeValue());

System.out.println("(node.getParentNode()).getNode Name():"
+
(node.getParentNode()).getNodeName());
System.out.println("============END=========\n");
}

//////////////////
public void postOrderTraversal(Node node)
{
int nodeType = node.getNodeType();

switch (nodeType)
{
case Node.DOCUMENT_NODE:
postOrderTraversal(((Document)node).getDocumentEle ment());

case Node.ELEMENT_NODE:
NodeList children = node.getChildNodes();

if (children != null)
{
for (int i = 0; i < children.getLength(); i++)
postOrderTraversal(children.item(i));

nodeInfo(node.getFirstChild());
}
}
}
//////////////////////

/** 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();

DocumentBuilderFactory
factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

doc = builder.parse(uri);

//doRecursive(doc);

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

System.err.println(ex.getClass());

System.err.println(ex.getMessage());
}
}
} //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_text" </menuitem>
<menuitem> feature="sing_text_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.getNodeValue(): feature="sing_text"
(node.getParentNode()).getNodeName():menuitem
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="sing_text_two"
(node.getParentNode()).getNodeName():menuitem
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="sing"

(node.getParentNode()).getNodeName():submenu
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="sub_pl_text"
(node.getParentNode()).getNodeName():menuitem
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="sub_pl"

(node.getParentNode()).getNodeName():submenu
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="pl"

(node.getParentNode()).getNodeName():submenu
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature="number"

(node.getParentNode()).getNodeName():submenu
============END=========
=======Start NODE INFO =========
node.getNodeValue(): feature ="n"

(node.getParentNode()).getNodeName():topmenu
============END=========
=======Start NODE INFO =========
node.getNodeValue():

(node.getParentNode()).getNodeName():menu
============END=========
Jul 17 '05 #1
2 4584
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(JMenuItem'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.hasChildren())
{
JMenu curMenu = new JMenu(current);
parent.add(curMenu);
for (child in current.getChildren())
{
visit(curMenu, child);
}
}
else
{
parent.add(new JMenuItem(current));
}
}

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.spam.com.spam> wrote in message news:<HB********************@twister.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(JMenuItem'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.hasChildren())
{
JMenu curMenu = new JMenu(current);
parent.add(curMenu);
for (child in current.getChildren())
{
visit(curMenu, child);
}
}
else
{
parent.add(new JMenuItem(current));
}
}

Ray

Jul 17 '05 #3

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

Similar topics

2
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...
2
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...
2
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...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
9
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
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
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...
2
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.