• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java Object to XML and back

 
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
paul wheaton
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know! How about something that does serialization, but it's all pumped into XML instead of a binary format!
 
paul wheaton
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JaxMe ???
[ March 17, 2004: Message edited by: Paul Wheaton ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Castor also does this, right?
Although, I don't know all the
diffs between things sugested here...
- m
 
paul wheaton
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Standard stuff in the JDK sounds like the smartest way to fly! Anybody see any shortcomings with this approach?
 
paul wheaton
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The generated XML from the stadard libs looks a bit .... bulky. Just me?
 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XStream is about as simple as it can get.
 
paul wheaton
Trailboss
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XStream seems like just the ticket! Anybody see any down sides to XStream?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23888
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ThoughtWorks is Martin Fowler's outfit, isn't it?
Yup, go all the way to the bottom....
- m
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the java docs for the xstream.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic