Programming - XML Java - comp.lang.java.programmer

Message 1 in thread
From: Martin Baker
Subject: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-22 02:32:02 PST

I would be grateful if anyone could give me ideas for the best way to
proceed.

My application has data which is stored in various classes which form a tree
like structure, these classes have their own application specific methods.

I would like to be able to:
* Serialise the classes to and from XML to store and retrieve the data.
* Act on this data using the classes application specific methods.
* Act on this data using a script engine.
* Display the data in a tree control.

I thought that the DOM standard would be a good way to do this as I could
implement the DOM interface in my own classes. However it seems that the
standard parsers and serialisers will only work with DOM classes which are
created in the provided factory methods and therefore I cant use my own
classes.

Any idea how I could use the built in libraries in java 1.4 to do this,
without reinventing functionality that has already been written by others?
Also I would prefer not to have to implement multiple tree-like interfaces
which all have similar functions but are different. Also I would like to
avoid requiring users to download separate class libraries.

Martin


Message 2 in thread
From: Chris Smith
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-22 07:00:23 PST

Martin Baker wrote ...
> I would be grateful if anyone could give me ideas for the best way to
> proceed.
>
> My application has data which is stored in various classes which form a tree
> like structure, these classes have their own application specific methods.
>
> I would like to be able to:
> * Serialise the classes to and from XML to store and retrieve the data.
> * Act on this data using the classes application specific methods.
> * Act on this data using a script engine.
> * Display the data in a tree control.

Looks to me like your best bet is to definitely stick with your own
application-specific adapters, use SAX to write the XML instead of
writing from a DOM, and write a custom TreeModel to adapt the data in
your classes to a Swing JTree. That doesn't take into account the
scripting engine, but I'm sure you can provide that access from app-
specific classes somewhere.

The benefit here is that you're definitely only maintaining one data
structure with the data in it, and everything else is communicating
through API adapters of various sorts. Your concerns are kept
separated.

The only requirement this will impose on your app data structures, for
the sake of the JTree, is that they be able to fire some kind of events
to indicate when and how they are being modified. Your TreeModel will
listen for those and turn them into TreeModelEvent instances that get
fired to the JTree.

> I thought that the DOM standard would be a good way to do this [...]

Nah. DOM is wonderful when your application's central point is actually
to work with an XML document of arbitrary structure, but its generic
data structures start to get in the way when you know more about your
data than the DOM parser does, and continue to get more in the way the
more application knowledge you start to build in.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Message 3 in thread
From: Martin Baker
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-23 01:43:05 PST

Chris,

Thanks very much for clarifying this, I will do what you suggest.

Its a pity there in not a standard interface for tree structures, but never
mind, I'll have to work with what there is.

Martin


Message 4 in thread
From: John McDonnell
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-24 12:33:59 PST


Martin,

Have you thought about using the java.beans.XMLEncoder/ java.beans.Decoder
classes in JDK 1.4? As long as your classes adhere to the JavaBeans spec,
they will be serialised correctly.

What sort of scripting support do you need? JScript should be able to handle
XML...

Writing a customer adapter class for a JTreeModel is straightforward, though
you could also consider wrapping your classes in DefaultMutableTreeNodes.

Cheers,

John.
Post a follow-up to this message


Message 5 in thread
From: Martin Baker
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-25 08:18:04 PST

John,

> Have you thought about using the java.beans.XMLEncoder/ java.beans.Decoder
> classes in JDK 1.4? As long as your classes adhere to the JavaBeans spec,
> they will be serialised correctly.

No I have not come across these classes yet, I do like this idea, there seem
to be so many tree-like interfaces in java (Beans, XML, JTreeModel, Java2D,
Java3D, etc.) It sounds like a good idea to implement Beans and get XML for
free. Its a pity they are not all derived from a common tree interface so
that I could implement it once and be able to display or output my classes
in any of these forms.
Are there any downsides to XMLEncoder/Decoder? the documentation seems to
suggest that it clones all the data (is this a deep clone?) which suggests
there may be memory size and performance issues as I may be working on large
files. I assume that the beans don't have to be visible beans (i.e. the type
that can be put in forms)

> What sort of scripting support do you need? JScript should be able to handle
> XML...

What I would like to do is to embed JScript into my file, in the same way as
IE and Netscape can run JScript embedded into HTML. In fact I seem to
remember that this is where DOM comes from? which is what made me think of
DOM originally. I also seem to remember that Rhino provides the ability to
run a script on any arbitary file structure and I think it may have special
support for Beans which is another bonus for implementing beans. It would be
good if there were a standard for this so, if I have a JScript which works
in HTML, I would not have to change it too much to use in my own program (ie
it would still have the same way of traversing the document data)

> Writing a customer adapter class for a JTreeModel is straightforward, though
> you could also consider wrapping your classes in DefaultMutableTreeNodes.

I have created my own base class which implements the MutableTreeNode
interface, my other classes are derived from this.

Thanks,

Martin


Message 6 in thread
From: John McDonnell
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-25 14:18:31 PST

"Martin Baker" wrote in message
news:b3g4u8$8nf$1@sparta.btinternet.com...
> John,
> No I have not come across these classes yet, I do like this idea, there seem
> to be so many tree-like interfaces in java (Beans, XML, JTreeModel, Java2D,
> Java3D, etc.) It sounds like a good idea to implement Beans and get XML for
> free. Its a pity they are not all derived from a common tree interface so
> that I could implement it once and be able to display or output my classes
> in any of these forms.
> Are there any downsides to XMLEncoder/Decoder? the documentation seems to
> suggest that it clones all the data (is this a deep clone?) which suggests
> there may be memory size and performance issues as I may be working on large
> files. I assume that the beans don't have to be visible beans (i.e. the type
> that can be put in forms)

Yep, if you've got deep/extensive object graphs, they will all get
serialized too, which may or may not be a problem - I would be more
comfortable if you would define 'large'.

> What I would like to do is to embed JScript into my file, in the same way as
> IE and Netscape can run JScript embedded into HTML. In fact I seem to
> remember that this is where DOM comes from? which is what made me think of
> DOM originally. I also seem to remember that Rhino provides the ability to
> run a script on any arbitary file structure and I think it may have special
> support for Beans which is another bonus for implementing beans. It would be
> good if there were a standard for this so, if I have a JScript which works
> in HTML, I would not have to change it too much to use in my own program (ie
> it would still have the same way of traversing the document data)

I've just been doing some work where we mapped XML schema(s) into Java
interfaces/classes using JAXB and also used XSLT from the XML files
themselves to generate web pages containing VBScript, which manipulated the
output - not quite the same, but similar.

Of course, Castor, Zeus and Quick can all do analagous mapping from schemas,
and I like Castor as it generates concrete classes, none of the fiddling
with interfaces that JAXB insists on, but I believe JAXB will become the
standard.

Anyway, this probably isn't advancing your cause, so good luck!

Cheers,

John.


Message 7 in thread
From: Martin Baker
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-26 08:17:31 PST

John,

> Yep, if you've got deep/extensive object graphs, they will all get
> serialized too, which may or may not be a problem - I would be more
> comfortable if you would define 'large'.

Some of my text files can be over 4Mb but I don't know what that means in
terms of RAM usage when the program is running.

> I've just been doing some work where we mapped XML schema(s) into Java
> interfaces/classes using JAXB and also used XSLT from the XML files
> themselves to generate web pages containing VBScript, which manipulated the
> output - not quite the same, but similar.

So there are at least 4 ways to read XML using Sun software alone. Do you
know of a good technical overview of JAXB? the document I found on the SUN
site appears to be more of a marketing document. I get the impression that
it allows you to define a mapping between a class structure and XML
elements, it sounds like there could be a performance issue here also, but I
would like to learn more.

Thanks again,

Martin


Message 8 in thread
From: John McDonnell (john@infonomicon.com)
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-26 12:29:31 PST

"Martin Baker" wrote in message
news:b3ip9c$kpk$1@venus.btinternet.com...

> So there are at least 4 ways to read XML using Sun software alone. Do you
> know of a good technical overview of JAXB? the document I found on the SUN
> site appears to be more of a marketing document. I get the impression that
> it allows you to define a mapping between a class structure and XML
> elements, it sounds like there could be a performance issue here also, but I
> would like to learn more.
>
> Thanks again,
>
> Martin

Martin,

If you download the JAXB beta, there's a pretty good user's guide in the
docs directory - choice of PDF or HTML.

It's actually the other way round to your statement :). Given an XML schema,
JAXB will automagically generate the necessary interfaces and classes which
will allow you to bind those classes to an instance XML document derived
from the schema. You can then (kinda, sorta) treat the XML document as a
persistent store for your Java objects. You have an ObjectFactory which you
have to use for object creation. You get marshalling, unmarshalling and
validation as part of the package. If you are using just straight XML, then
it's probably not germane, and it's definitely not for you if you are
writing a document-centric application.

Cheers,

John.


Message 9 in thread
From: Martin Baker
Subject: Re: How can I implement XML?
Newsgroups: comp.lang.java.programmer
Date: 2003-02-27 08:02:57 PST

John,

Thank you very much for you help, I will follow up on the options you
suggested. Even if I end up using SAX for this application it is still
useful to understand all the options.

Martin


metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover XML Pocket Consultant - A good reference book, includes XSL, XML schemas, XLink and Xpath (Microsoft book)

cover XML in a Nutshell - includes XSLT & XPath, XLINKS, XPointers, CSS & XSL

cover Learning XML - good chapter on XSLT

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual C# .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2015 Martin John Baker - All rights reserved - privacy policy.