Two Laptop Bag*
The moose likes XML and Related Technologies and the fly likes Java Object to XML and back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Java Object to XML and back" Watch "Java Object to XML and back" New topic
Author

Java Object to XML and back

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

Seems like there should be something that can use reflection to look at an object and write out an XML. And then rebuild the object using XML. And it might not hurt to have a bit of an xdoclet like thing thrown in to talk about mapping particulars.
Anybody know of such a beast?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

I know! How about something that does serialization, but it's all pumped into XML instead of a binary format!
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

JaxMe ???
[ March 17, 2004: Message edited by: Paul Wheaton ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Jakarta Digester
JAX-B (XML binding)
java.beans.XMLEncoder
JOX (almost forgot this one)
There are quite a few of these things ... each of them has good and poor aspects. It's all a matter of balance. The three above are a few that I know of, but seeing as how just about everybody loves XML these days, serializing beans to XML is just about everywhere and the libraries for them too.
[ March 17, 2004: Message edited by: Nathaniel Stoddard ]

Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
There are a couple of classes already in the standard java 1.4 distribution:
java.beans.XMLDecoder and java.beans.XMLEncoder
http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLDecoder.html
http://java.sun.com/j2se/1.4.2/docs/api/java/beans/XMLEncoder.html
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Castor also does this, right?
Although, I don't know all the
diffs between things sugested here...
- m
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

Standard stuff in the JDK sounds like the smartest way to fly! Anybody see any shortcomings with this approach?
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

The generated XML from the stadard libs looks a bit .... bulky. Just me?
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Check out skaringa -
The default serialization settings isn't that "readable" but you can configure it to output something pretty sparse. I use it all the time in my apps and it works like a charm. Handles pretty much any type of object, and can generate schemas based on a Class.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
I don't like the java.beans.XMLEncoder either -- it's too messy. Plus, if you start with an XML file, there's a chance it may not be able to correctly serialize it both ways.
JAX-B is just psychotic. You need to create an XML schema firstly. Then you generate a DOM-like package full of all your classes. But it too is pretty crazy, with names that just aren't too easy on your brain cells.
Digester as far as I know only will serialize one way (XML->Beans), but it's pretty flexible and decent. There's a definite de-coupling between the serialization mechanism and your Beans --- something that is definitely missing with the previous two approaches.
JaxMe, JOX, Castor I haven't used. I think Castor is actually build on another Jakarta (or Apache) project that actually does the Bean/XML serialization, but you'll have to verify that.
Then of course, you could always just write your own serialization routines with the DOM, but that's just a last resort -- one of these solutions will most likely fit your needs well.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
XStream is about as simple as it can get.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

XStream seems like just the ticket! Anybody see any down sides to XStream?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Paul Wheaton:
Anybody see any down sides to XStream?
It has zero javadocs available... (not that there would be many public methods needing explanation, but still)
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

Originally posted by Lasse Koskela:
It has zero javadocs available... (not that there would be many public methods needing explanation, but still)

This looks a bit non-zero: http://xstream.codehaus.org/apidocs/index.html
ThoughtWorks is Martin Fowler's outfit, isn't it?
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
ThoughtWorks is Martin Fowler's outfit, isn't it?
Yup, go all the way to the bottom....
- m
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Paul Wheaton:
This looks a bit non-zero: http://xstream.codehaus.org/apidocs/index.html

Sure, I would see that information with Eclipse's auto-complete window as well. The point was that there's no explanations within those javadocs...
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Here is the java docs for the xstream.


Kishore
SCJP, blog
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Kishore Dandu:
Here is the java docs for the xstream.

Yes, and they're still missing the comments that I mentioned about earlier...
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Lasse Koskela:

Yes, and they're still missing the comments that I mentioned about earlier...


Many enterprise systems warrant custom XSD compliance(atleast on a production level for interoperability). Xstream can not do that.

In that situation u may have to go with xmlbeans or castor or jaxb.
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
I did some research. Options were Digester, Castor and JAXB. Most comparisons on web are written for old JAXB.

Digester - AFAIK it does XML -> Java Object but not vice versa.
Castor - people have been using it for some time. Good but no validation.
JAXB - I think it's the hottest technology. + Validation.

Best regards,
Vladas
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Vladas Razas:
Digester - AFAIK it does XML -> Java Object but not vice versa.

Huh? Why would anyone use Digester if you can't read the generated XML schema back to Java objects? Anyway, Digester does indeed support the full round-trip.
 
 
subject: Java Object to XML and back
 
Similar Threads
XML "object mapping"
XMLDOM to String Representation
Converting an object into an XML
How to create an xml from data object
Spring 3 and XSLT