Parsing a Schema to build a JTree

Discussion in 'Java' started by Dj Frenzy, Nov 27, 2004.

  1. Dj Frenzy

    Dj Frenzy Guest

    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
    Dj Frenzy, Nov 27, 2004
    #1
    1. Advertising

  2. Dj Frenzy

    Chris Smith Guest

    Dj Frenzy <> wrote:
    > 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
    Chris Smith, Nov 27, 2004
    #2
    1. Advertising

  3. Dj Frenzy

    Guest

    Chris Smith wrote:
    > Dj Frenzy <> wrote:
    > > 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
    , Dec 16, 2004
    #3
  4. Dj Frenzy

    Chris Smith Guest

    <> wrote:
    >
    > 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
    Chris Smith, Dec 16, 2004
    #4
  5. On Thu, 16 Dec 2004 15:15:07 -0700, Chris Smith wrote:

    > <> wrote:
    >>
    >> 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. ;)

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
    Andrew Thompson, Dec 16, 2004
    #5
  6. Dj Frenzy

    Guest

    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
    , Dec 17, 2004
    #6
  7. Dj Frenzy

    Chris Smith Guest

    <> wrote:
    > 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
    Chris Smith, Dec 17, 2004
    #7
  8. Dj Frenzy

    Guest

    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.
    , Dec 18, 2004
    #8
  9. Dj Frenzy

    Guest

    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
    , Dec 26, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,481
    Markus
    Nov 23, 2005
  2. Klaus Johannes Rusch

    Re: Convert DB2 schema to XML Schema

    Klaus Johannes Rusch, Aug 6, 2003, in forum: XML
    Replies:
    0
    Views:
    547
    Klaus Johannes Rusch
    Aug 6, 2003
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,159
    Stanimir Stamenkov
    Apr 25, 2005
  4. Replies:
    2
    Views:
    2,093
  5. jumpz
    Replies:
    3
    Views:
    606
    Jean-Baptiste Nizet
    Nov 12, 2007
Loading...

Share This Page