By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

postorder traversal of a DOM tree

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.