This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

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

 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The root element of the document is named "ProblemBean" not "Problem".
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about the .class?
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I do not understand ".class". Please make it clearer.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 27900
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 27900
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 don't like that guy. The tiny ad agrees with me.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic