XML & Web services: XML to runtime java objects- dynamic conversion?
- Posted by: Ramnath Putta
- Posted on: January 15 2003 11:01 EST
I am in need to convert the XML file in to appropriate java runtime objects..
If I give this input file as input, the Objects of type Person and Address is desired to be filled with data and come out as output. Is there any way to do it?? are there libraries already available???
Would be grateful if you guys reply to this message.
- XML to runtime java objects- dynamic conversion? by Weston Aiken on January 15 2003 12:28 EST
- XML to runtime java objects- dynamic conversion? by Lasse Koskela on January 15 2003 15:21 EST
- XML to runtime java objects- dynamic conversion? by Gal Binyamini on January 15 2003 19:21 EST
- XML to runtime java objects- dynamic conversion? by Vivek Rai on January 16 2003 10:57 EST
- XML to runtime java objects- dynamic conversion? by vimarsh vasavada on January 20 2003 04:04 EST
Why not DOM?
Why not SAX?
No, seriously, I'd suggest you lookup some SAX parsing tutorial and code a handler class, which populates the whatever-objects. There's probably sample code which is very close to what you need...
Good to hear from you. Thank you very much Gal and Vivek. Both the libraries suggested by you was really useful to me. The class
JOXBeanBuilder class from the project (http://www.wutka.com/jox.html) exactly does what I want :).
and ofcourse the method
Object readObject(Class obClass)
forms the dynamic instance of the class template input, using the XML file supplied. Excellent and powerful.
I have one more small issue... This JOXBeanBuilder fills the value in to the bean using its getter and setter method at runtime. Out of curisoity, I want to know, if such kind of builder is available for normal classes also (my current classes are in these forms :( ..possibly you can treat those classes as a kind of structures in C which does nothing except to hold the data).
Actually, the runtime representation of an XML document (or fragment) should probably not be DOM.
If you plan to use the object more than once, a DOM instance requires that its handlers burrow down the node tree searching for Strings in each case. If the object is to be used extensivley this is a maintainence nightmare (someone changes the name of an attribute and _you_ have to chase down all the occurences of that String).
A better approach in my opinion is to maintain a tight object binding as the xml comes in the door. The problem with this is its lack of generality - every type of document, or fragment of document, that you will be able to handle must be determined in advance.
So what I would do is write a generic data-bean factory to convert an XML heirarchy into a data-bean. Whether you use containment (one bean per element) or some more efficient optimisation, you can then use the bean without the code-rot that passing DOMs throught system engenders.
Alternatively, if you are writing over the wire from java to java, you could prepare data-beans to send, then use the standard java.beans.XMLEncoder and java.beans.XMLDecoder to write and read the beans to and from XML.
thats right, best for data binding. castor.org
See http://jxv.sourceforge.net (an open-source project of mine, might be relevant).
Check this out this might be helpful to u .
This does excatly the same u want to achieve. this site references some other work on the same line.
hope this helps.
You can use Castor. This is very commonly used for java object and xml binding, and the best part is its an opensource.