Parsing a Schema to build a JTree

D

Dj Frenzy

Hi, I'm writing an XSLT generation tool in Java, and at the moment and
working on a facility to display the hierarchical structure of a XML
Schema.

I know there are examples availible on the net for building a JTree
from an XML file, but does anyone know of any existing programs which
will build a JTree for a schema?

Cheers
David
 
C

Chris Smith

Dj Frenzy said:
Hi, I'm writing an XSLT generation tool in Java, and at the moment and
working on a facility to display the hierarchical structure of a XML
Schema.

I know there are examples availible on the net for building a JTree
from an XML file, but does anyone know of any existing programs which
will build a JTree for a schema?

I can't point you to publically available code, but I have done
something very close to this and can give advice. Specifically, you
ought to write your own model class instead of using DefaultTreeModel,
and use org.w3c.dom.Element directly as your tree node objects. If you
use a parser that implements the DOM Events spec, it's fairly simple to
have your model listen for events on the DOM document and translate them
into the appropriate TreeModelEvent.

Finally, you'll need to install a TreeCellRenderer that's capable of
drawing the items in the way you want (since DOM Element instances
aren't guaranteed to override toString()).

This isn't terribly difficult, and I can answer any questions you have
here on the newsgroup.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

dado0583

Chris said:
I can't point you to publically available code, but I have done
something very close to this and can give advice. Specifically, you
ought to write your own model class instead of using DefaultTreeModel,
and use org.w3c.dom.Element directly as your tree node objects. If you
use a parser that implements the DOM Events spec, it's fairly simple to
have your model listen for events on the DOM document and translate them
into the appropriate TreeModelEvent.

Finally, you'll need to install a TreeCellRenderer that's capable of
drawing the items in the way you want (since DOM Element instances
aren't guaranteed to override toString()).

This isn't terribly difficult, and I can answer any questions you have
here on the newsgroup.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Chris Smith wrote:

[...]

Yes, I did write that. Did you have a question or comment? You seem to
have sent this without adding your own content.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

Chris Smith wrote:

[...]

Yes, I did write that. Did you have a question or comment? You seem to
have sent this without adding your own content.

Maybe dado0583 thought it was so important that it was worth
repeating exactly as is, for emphasis. ;)
 
D

dado0583

Hey, I typed out a whole long message last night, but unfortunately it
screwed up a bit.

I haven't had much experience using Java with XML. I've come up with a
quite fix using JAXB, but as this is quite a big part of the software
that I'm making I want to make it faster, because my current solution
takes about 5-10 seconds to build a JTree based on a small XSD file.

Your suggestion, is it basically saying to use a DOM parser to build a
number of org...Element objects in a tree style, then build the JTree
from that. I'm a little confused by your answer.

As for a TreeCellRender, whoosh, thats gone straight over my head. I'm
not looking to manipulate the tree once its been built. It's just there
for reference for the user, so if thats what it's used for then I guess
it doesn't really apply for me.

Also, I tried installing Xerces DOM parser last night, but I couldn't
manage to cos I'm a bit thick or something. Do you know of a
step-by-step guide to installing any DOM parser?
Cheers for the help,
Dave
 
C

Chris Smith

Your suggestion, is it basically saying to use a DOM parser to build a
number of org...Element objects in a tree style, then build the JTree
from that. I'm a little confused by your answer.

What I said was to parse the XML document to a DOM and then, not build a
tree from that, but write a TreeModel implementation based on it. For
example:

public class DOMTreeModel implements TreeModel
{
private org.w3c.dom.Document document;

public DOMTreeModel(org.w3c.dom.Document doc)
{
this.document = doc;
}

// ... implement TreeModel methods here ...
}

The type for a tree node in TreeModel is Object, so you can return
anything. The best plan is to use Node instances from the DOM as tree
nodes. Each of the TreeModel methods will call methods in the document
or on the Node object (which you'll need to cast after it's passed in)
and return the appropriate information.

If (and only if) you intend to change the document while it's displaying
in a tree, then you also need to use the DOM Events specification, and
convert any DocumentEvent from the DOM level into a TreeModelEvent. I
suggested Xerces because Java 1.4's JAXP implementation doesn't provide
events. It appears, from a look at the API documentation, that 1.5
does. If you're using 1.5, you may not need to bother with Xerces.
Also, if you don't intend to change the document as you're displaying
it, then you don't need to bother with any of this.
As for a TreeCellRender, whoosh, thats gone straight over my head. I'm
not looking to manipulate the tree once its been built. It's just there
for reference for the user, so if thats what it's used for then I guess
it doesn't really apply for me.

TreeCellRenderer is necessary, but it's not hard. Here's a simple
example:

public class DOMTreeCellRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus)
{
org.w3c.dom.Node node = (org.w3c.dom.Node) value;
String text = node.getNodeName();

return super.getTreeCellRendererComponent(
tree, text, sel, expanded, leaf, row, hasFocus);
}
}
Also, I tried installing Xerces DOM parser last night, but I couldn't
manage to cos I'm a bit thick or something. Do you know of a
step-by-step guide to installing any DOM parser?

Xerces can be used just like any other Java library. Once you install
it, you can parse a document by creating an instance of
org.apache.xerces.parsers.DOMParser, and calling its parse method.
Again, though, if you're using 1.5 or don't need to keep the display up
to date with changes to the document, then you can skip this step.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

dado0583

Wow, cheers. I think I have a reasonably good idea of what needs doing
now and will probably get on to doing some more later on today. Thanks
alot Chris and I'll probably get back to you again.
 
D

dado0583

hey, I've practically finished the JTree thing now. And I was hoping
you could help me with a slight challenge.

Basically, when the tree is built there may be a subtree called
USAdress, with name, street, etc as children nodes. Elsewhere in the
tree, there are nodes called shipTo and billTo (which are defined as
USAddress types as defined in the given schema). In my current
algorithm, the USAdress node is added to shipTo and then to billTo, so
the USAddress node is moved to billTo, and is no longer the child of
shipTo.

Is there any easy way to make a copy of a TreeNode (and its subtree)?
Ie. is there an existing method which will work this magic for me, or
am I going to need to write one?

Cheers,
David
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top