Displaying a DOM tree using a Jtree

Status
Not open for further replies.

Sivaprasad

Usenet Archive
Joined
May 22, 2002
Messages
3
Reaction score
0
Hi,
I wan't to read an XML document from a file and display it in a java application using Jtree.I am using the treewalker class to traverse the document.I am able to see everything(elements,text node..etc) except the Attributes in the Jtree.This happens even after i have given the 'whattoshow' parameter as 'SHOW_ALL' in the following line (used in the code) TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, true);

Is there any so that the attributes also can be displayed?Can any one help me out?

Thanks & Regards
Sivaprasad

The following is the code i used.
public class DOMTreeWalkerTreeModel implements TreeModel { TreeWalker walker; public DOMTreeWalkerTreeModel(TreeWalker walker) { this.walker = walker; } public DOMTreeWalkerTreeModel(Document document) { DocumentTraversal dt = (DocumentTraversal)document; walker = dt.createTreeWalker(document, NodeFilter.SHOW_ALL,null,false); } public DOMTreeWalkerTreeModel(Element element) { DocumentTraversal dt = (DocumentTraversal)element.getOwnerDocument; walker = dt.createTreeWalker(element, NodeFilter.SHOW_ALL, null,false); }

public Object getRoot { return walker.getRoot; }
public boolean isLeaf(Object node) {
walker.setCurrentNode((Node)node);
Node child = walker.firstChild;
return (child == null); }

public int getChildCount(Object node) {
walker.setCurrentNode((Node)node);
int numkids = 0;
Node child = walker.firstChild; // Start with the first child while(child != null) { // Loop 'till there are no more numkids++; // Update the count child = walker.nextSibling; // Get next child } return numkids; // This is the number of children }

public Object getChild(Object parent, int index) {
walker.setCurrentNode((Node)parent);
Node child = walker.firstChild;
while(index-- > 0) child = walker.nextSibling;
return child;
}

public int getIndexOfChild(Object parent, Object child) { walker.setCurrentNode((Node)parent); // Set current node int index = 0; Node c = walker.firstChild; // Start with first child while((c != child) && (c != null)) { // Loop 'till we find a match index++; c = walker.nextSibling; // Get the next child } return index; // Return matching position }

public void valueForPathChanged(TreePath path, Object newvalue) {} public void addTreeModelListener(TreeModelListener l) {} public void removeTreeModelListener(TreeModelListener l) {}

public static void main(String args) throws IOException, SAXException { DOMParser parser = new org.apache.xerces.parsers.DOMParser;

Reader in = new BufferedReader(new FileReader(args[0])); InputSource input = new org.xml.sax.InputSource(in); parser.parse(input); Document document = parser.getDocument;

DocumentTraversal traversal = (DocumentTraversal)document;

NodeFilter filter = new NodeFilter {
public short acceptNode(Node n) {
if (n.getNodeType == Node.TEXT_NODE) { // Use trim to strip off leading and trailing space. // If nothing is left, then reject the node if (((Text)n).getData.trim.length == 0) return NodeFilter.FILTER_REJECT; } return NodeFilter.FILTER_ACCEPT; } };

int whatToShow = NodeFilter.SHOW_ALL;

TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, false);

JTree tree = new JTree(new DOMTreeWalkerTreeModel(walker));

JFrame frame = new JFrame("DOMTreeWalkerTreeModel Demo"); frame.getContentPane.add(new JScrollPane(tree)); frame.setSize(500, 250); frame.setVisible(true); } }
 

Robin Rosenberg

Usenet Archive
Joined
May 22, 2002
Messages
2
Reaction score
0
sivaprasad wrote:

Hi,
I wan't to read an XML document from a file and display it in a java application using Jtree.I am using the treewalker class to traverse the document.I am able to see everything(elements,text node..etc) except the Attributes in the Jtree.This happens even after i have given the 'whattoshow' parameter as 'SHOW_ALL' in the following line (used in the code) TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, true);

Is there any so that the attributes also can be displayed?Can any one help me out?

Attribute aren't child nodes in the tree, even though they inherit from Node. You must handle them explicity.

-- robin
 
Status
Not open for further replies.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top