wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean" Watch "Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean" New topic
Author

Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
This is the first time I use JAXB. I have very simple files (i.e., doing something very simple). I first converted a Java bean to a XML file using JAXB Marshaller. It was done without problem.

Thereafter, I tried to convert that XML file back to a Java Bean using JAXB Unmarshaller - I prepared a XSD file and used XJC to compile that XSD file, which generated two classes inclusing an ObjectFactory.java. When I tried the JAXB unmarshaller, I got an error message:
Exception in thread "Main Thread" javax.xml.bind.UnmarshalException - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'problemBean'.]

Please help me to understand where went wrong. I am going to show the source code for my Java Bean, marshaller class, xsd file, and unmarshaller class below:

I first converted a Java Bean (ProblemBean.java) to a XML file (ConvertedFromProblemBean.xml) with some success:

My JAXB marshaller class:


I then tried to convert the new XML file back to Java Bean. Here is the very simple XSD file (problem.xsd):

I have two Java classes generated after I used XJC to compile the problem.xsd. I then have a JAXB Unmarshaller class:
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
The root element of the document is named "ProblemBean" not "Problem".
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Thanks for your reply to many of my posts.

The error was resolved in a weird way, I have to change the root element in my xsd file to (note: the lower case "p" )

The uppercase "P"roblemBean does not work. I wonder what the reason is.

Now, I have another question: I have another error
Exception in thread "Main Thread" java.lang.ClassCastException: generated.ProblemBean
at gov.va.bhie.jaxb.beanXML.conversion.XML2BeanDemo.main(XML2BeanDemo.java:29)

The message says that a class cast problem occurred in the "generated" folder with the ProblemBean.java and code below is my gov.va.bhie.jaxb.beanXML.conversion.XML2BeanDemo.java

However, if I code as there is no error message. Would you please advise? Thank you.

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
[0] The proper naming of the element in xsd file is not thing negotiable. You've to keep the change.

[1] Then you've to examine rigoriously all the paths to xsd, xml are correct. And then make sure the directory "generated" (the default package name) is within reach via classpath setting. If it is at the first level subdirectory of the current directory, then it is fine as well.

[2] In the above are all in place, you have this final detail of utmost importance still to change as to the proper object typing.

With it you can access properties via o.getAcuity() etc signalling the unmarshalling is done correctly.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
[0.1] Sorry, I haven't been very clear. What I meant is the name in the xsd must coincident with what is generated. In fact, jaxb generates xml with some adjustment by default such as leaving the first letter in lower case, also such as collapsing underscore etc... Hence, the exact name is something you can do a simple inspection to know. And, I think, your saying "problemBean" is obvious the one you've to keep. It is what the marshaller would generate by default. You should not be surprised on the problemBean vs ProblemBean because if you inspect the xml generated, you would surely discover that.

[0.2] If you really want to keep the upper case at the first letter, you can sure edit the XmlRootElement annotation, like this.

Hope that clarify further on this point.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
By re-reading now your post, I come to the conclusion that I must have prepared my response based on some unedited version of your original post. Something just does not match up... It doesn't matter! Just make a note here.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Thanks for your continuing support and quick response.

I have modified the code to I still get the same ClassCastException. I think the directory structure of my very simple first project is in proper order (shown below):

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
How about the .class?
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Sorry, I do not understand ".class". Please make it clearer.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Aside, in many cases it is much easier to create your own binding implementation than to use JAXB, XML Beans, XML Schemas, etc. It is as simple as creating a series of toXML() methods in your data classes. And then create a SAX application to read an XML file an populate your data objects. This will give you complete control over everything.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Natalie Kopple wrote:I have modified the code to I still get the same ClassCastException. I think the directory structure of my very simple first project is in proper order (shown below):



Notice that you have ProblemBean twice in that hierarchy. Your large program which tries to do the unmarshalling doesn't import generated.ProblemBean, so it must be using gov.va.bhie.jaxb.beanXML.conversion.ProblemBean. But apparently the unmarshalling process produces a generated.ProblemBean object (or so the error message leads me to believe). Such an object cannot be casted to gov.va.bhie.jaxb.beanXML.conversion.ProblemBean, the two are completely different classes.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
>Sorry, I do not understand ".class". Please make it clearer.
I don't know what to say. If all things are not in an archive, there are .class files all over the place, aren't there?

Somewhere, in a certain "generated" directory there are two class files.
x/generated/ProblemBean.class
x/generated/ObjectFactory.class

Make sure the directory x is in the classpath setting. If x is the current directory, you can spare that need. But let me show you what I can deduce your current directory is:
From the line
Schema schema = schemaFactory.newSchema(new File("gen_source/problem.xsd"));
and
marshaller.marshal(problem, new FileWriter(".\\src\\ConvertedFromProblemBean.xml"));
These lines suggest your current directory when executing the application is PrototypeProblemBeanTransformation. Then it is not correct if "generated" directory that contains the ObjectFactory.class and ProblemBean.class (not that contains .java) is not directly under it (ie, x being PrototypeProblemBeanTransformation) and no specific classpath setting is devised to point to x. In factor the "generated" can be placed anywhere as long as the parent directory is in the classpath. If the "grandparent" (y) is in the classpath, like this:
y/x/generated/ObjectFactory.class
y/x/generated/ProblemBean.class

Then the import and JAXBContext lines should be reflecting that.

I can assure you the marshalling and unmarshalling work just fine in this simple case. All the more delicate elements are mentioned in the couple of posts above. The rest is all your diligence and seriousness of taking your work.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Thank you and sorry for taking so much time from all of you.

The mistake I made is that I put both Marshaller and Unmarshaller in the same package. I first ran the Marshaller. When I ran the Unmarshaller, the compiler was confused by two versions of the ProblemBean.java - one was created manually and the other was generated.

Nevertheless, I think that I have a basic understanding of how JAXB works after this exercise.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Natalie Kopple wrote:Nevertheless, I think that I have a basic understanding of how JAXB works after this exercise.


That's a significant achievement, actually. It seems to me that JAXB is one of those things they made too hard for regular people to understand, although maybe that's because they based it on XML Schema. Anyway, congratulations!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean
 
Similar Threads
How to Add a Line Counter for Each XML Element in XSL?
Exception while deploying webservice on jboss 4.2.1 using jax-ws
XML not getting validated with schema... + strange...
XML generation pbm with JAXB Marshaler
problem with validating sax parser