Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaBean to XML and XML to JavaBean  RSS feed

 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Is there a utility/an API that converts JavaBean to XML and vice versa?

There are some javabeans that contains other javabean in a collection. I need to convert them into XML and store the XML in the database.

Any input from everyone is helpfull,

Thanks in advance,

Pinal.
 
Ed Wallen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many libraries that have this functionality. Here are a couple of the more popular ones:

Castor XML: http://www.castor.org/

JAXB: http://java.sun.com/xml/jaxb/

-Ed
[ March 04, 2005: Message edited by: Ed Wallen ]
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another XMLBeans by Apache Software Foundation.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found JAXB rather evil on my project. It mechanically generates a class for any tag that has nested tags and with complex XML it can generate hundreds and hundreds of classes. I guess that's ok if you treat it as a black box and never look inside, but we couldn't quite do that.

We parsed XML to JAXB-generated classes, then mapped from those classes to application standard business objects. The mapping was icky work, so I threw out JAXB and hand wrote SAX handlers to parse straight to the required business objects. It's actually less hand-written code than the mappers. You're welcome to my base Reusable Sax Parser class.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, the core Java API provides this now. Check out java.beans.XMLEncoder and java.beans.XMLDecoder. They work quite well, actually. Enjoy!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, it sounds like we may be solving different problems. If you encode from Java to XML, pass the XML around or store it, then decode back to Java - all without looking too closely at the XML - an automated tool may do beautifully for very low effort.

We are trying to consume XML produced by a partner system web service. Their XML is an overly intricate vertical industry standard plus nonsensical tags like "CustomerExtension" so the automated tools didn't give us meaningful Java output.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
James, it sounds like we may be solving different problems. If you encode from Java to XML, pass the XML around or store it, then decode back to Java - all without looking too closely at the XML - an automated tool may do beautifully for very low effort.

We are trying to consume XML produced by a partner system web service. Their XML is an overly intricate vertical industry standard plus nonsensical tags like "CustomerExtension" so the automated tools didn't give us meaningful Java output.


Weren't we both trying to solve Pinal's original problem and not two different ones? :-) You're right, the XMLEncoder/XMLDecoder probably wouldn't be very useful in your situation. But, it seems that they need to store a JavaBean in a String column in a database. For that simple purpose (as long as the JavaBean classes are structured properly), the XMLEncoder/XMLDecoder will work fine. Also, changes in class format (adding/removing fields) doesn't affect the encoding/decoding process all that much. It's quite robust. I wrote a class on my current project which helps out tremendously with what they are attempting...





The equalsXmlEncoded() method is useful for testing your encoding/decoding to make sure your beans will serialize properly. In a unit test, you would do something like...



You will have to make sure that you add things to the nested collections and write reasonable equals() and hashCode() (always override hashCode() id you're overriding equals()) methods. Hope this helps!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weren't we both trying to solve Pinal's original problem and not two different ones?


I think different larger goals with a common technique in the middle. The original post is a bit ambiguous about which way he's going.

I'll have to look at encode/decode ... I might have a good place for that. Have you ever used Glue? It converts Java to XML and back for remote SOAP calls with about two lines of extra code on both ends and you never have to see the XML. Graham Glass knows how to be good to programmers.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used Glue before, but I have heard about it and I just went to the website. I looked at the "Web Services in 10 mins. or less" tutorial and it looks pretty cool. It definitely does make it easy to expose an object as a web service. Can you use the part of the framework that just encodes/decodes the objects by itself? In this particular case, we merely wanted to save objects into a database as XML Strings, not pass them back and forth as parameters/return values to/from a method as XML.
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outstanding information. Thanks to all of you. James, I ended up using the XML Encoder/ Decoder and it made my life much easier.

I appreciate all of your input in this. I have learned few things more than what I was looking for. So, thanks a lot guys !!
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

ONE MORE QUESTION...

While trying to decode a XML string into an specific object using XMLDecoder.readObject() method, I get the below exception
'java.lang.NoSuchMethodException: <unbound>= fooTO.setName("lala")'

The reason I get the exception is due to the fact that there is no "set" method called "setName" on fooTO javabean class.

What I am curious is if there is anyway I can just simple ignore the methods that is not in the javabean class and proceed successfully creating an Object from XML string without throwing any exception.

I REALLY APPRECIATE EVERYONES INPUT.

THANKS IN ADVANCE
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another very simple and rapid one: XStream.

--
:alex |.::the_mindstorm::.
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried XStream api as well and got the same result. This time with its own custom exception. Below is the custom exception, Please let me know if I am doing something wrong...

"joinPlug" is one of the property in the javabean class that does not exist anymore, but was exisiting during the creation of the XML but not during converting XML back to the javabean object. So, basically once the XML is created, the XML is persisted in the database as a string...after few days the javabean that was used to create the xml has been refactored (in this case...deletion of some property), so when I try to decode the XML string back to the object I get the following error...


com.thoughtworks.xstream.converters.ConversionException: joinPlug : joinPlug
---- Debugging information ----
required-type : org.cas.emmy.domainservice.patent.retrieve.to.PatentDocumentTO
cause-message : joinPlug : joinPlug
class : org.cas.emmy.domainservice.patent.retrieve.to.PatentDocumentTO
message : joinPlug : joinPlug
path : /org.cas.emmy.domainservice.patent.retrieve.to.PatentDocumentTO/joinPlug
cause-exception : com.thoughtworks.xstream.alias.CannotResolveClassException
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a little experiment with what you are describing. First I did this...



Then, I did this...



Notice how I removed the property B before I attempted to read the object back out of the file. It did have a problem setting the B property, but it did continue. Here's the output...


java.lang.NoSuchMethodException: <unbound>=PassByValue$MyBean0.setB("Some B String");
Continuing ...
Some A String


So, it's okay if a class has one of its properties removed (notice that successfully created a MyBean object and it actually did print the value of the A property). It will continue on, setting the properties that it can during "deserialization".
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that make sense. It exception is treated as a warning. thx James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!