users@jaxb.java.net

Re: Converting XML to Java Objects...possible?

From: Raymond Wold <raymond.wold_at_teletopia.com>
Date: Tue, 10 Apr 2007 08:28:48 +0200

tweety123 wrote:
> Hi, As far as I know about JAXB, it provides the facility to convert "XSD"
> (not XML) to Java "classes" (not objects). This can be done using xjc
> compiler. Can XML files (i.e. ones with data) be converted to Java objects
> (retaining the data from XML files)? If yes, please throw some light on how
> it can be done. If no, then is there some java technology to do such a
> thing?
>
> Thanks.

Java is a typed language. To have an object, you have to have a class. To have
a class, you have to know in advance what kind of data it will contain, and
how to access it. Usually if you want unmarshalling, and don't have an XSD for
your XML documents, you write the XSD yourself (to specify what kind of data
the documents can contain; this has other advantages as well) and then run xjc
on it.

This is all a bit simplified as you can circumvent javas type safety by
creating classes on the fly, and using introspection to create and read the
instances. Not something you want to do though.

If all you want is to know how to read XML data in java (in other words,
unmarshalling is not necesarilly what you are looking for), have a look at the
DOM and SAX APIs. See http://java.sun.com/xml/tutorial_intro.html