This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Clarification Requested - XML recordset & JAXB classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Clarification Requested - XML recordset & JAXB classes" Watch "Clarification Requested - XML recordset & JAXB classes" New topic
Author

Clarification Requested - XML recordset & JAXB classes

Karen Jonathon
Greenhorn

Joined: Jul 25, 2011
Posts: 5
I need help in figuring out an approach to my problem (New to Java, competent in XML).

1.) I have XSD file that I have no control over. It is a complicated XSD file that I converted to java classes using JAXB.

2.) I have a database recordset in XML format. An example of the file is as so:



What I need to accomplish:
Map the data from the XML recordset file to the JAXB classes. I have trouble figuring out how to do this, although I know it must be something obvious that I am missing.

Jaxb created several classes from the XSD. The JAXB derived class Item.java is below


Would I have to read my XML recordset as a DOM object, parse it into these JAXB setters?


Is that the right approach? Please advise.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
>Would I have to read my XML recordset as a DOM object, parse it into these JAXB setters?
You shouldn't need to make it to a dom tree to bind the data to your class... jaxb unmarshaller should do it for you.

Nevertheless, if your xml documents are tagged like what you've shown, you need to properly annote the classes further. This is what I see that needed done by just browsing through your Item class.
Karen Jonathon
Greenhorn

Joined: Jul 25, 2011
Posts: 5
Thank you so much for your help.

I have changed my code as you advised

I annotated my JAXB derives class as follows:

My Main Code:



My XML output:




Since the XML input with names catalog_num, catalog_descrip are field names, it is a requirement that the xml output maintains the xml element names in the xsd. So, I would need to maintain the xml element names: code, name, price. I would need a result like below:




Karen Jonathon
Greenhorn

Joined: Jul 25, 2011
Posts: 5
I am about to try this out, but I believe the below link fills in the gap:

Mapping Java Objects and XML Documents using JAXB

*Update*
It seems I may have misunderstood this. I will keep searching. . .
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
If I understand the scope of the problem correctly, it is that your xml input to be unmarshalled is constructed with tags like inventory, catalog_num,... etc. And that, you want to provision anotated classes that bind to it. Whereas, you want, after doing whatever such as editing or else with the pojo, marshal it to output xml document with tags like Item, code,... etc.

You can do this with two sets of classes (compiled from two set of .java). One with annotations including name="inventory" etc (within the package "items.exec"), and the substantively the same set of classes but without additional annotations of name (like what of your original post within another package "generated" or whatever.
Karen Jonathon
Greenhorn

Joined: Jul 25, 2011
Posts: 5
g tsuji, you thoroughly explained what I am attempting to accomplish.

I appreciate your patience with me.

I have 2 packages, the items.exec - that have the annotations. And generated, that do not have annotations. I have changed my Main to the following:



When I run this, I get the following error:

The error is at the following line of code:


g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
Hi Karen Jonathon, I am glad you've got the essense of the idea and the layout is in fact quite good in putting the idea to test. That shows you know the business. But, there are some bridging ideas that I have not mentioned and they are not very much expanded in the official demo implementation of jaxb, leaving so much to desire.

>I have 2 packages, the items.exec - that have the annotations. And generated, that do not have annotations.
The main bridge one has to build is to convert a JAXBElement from one package (type) to another. Without such conversion, it would result in error you've mentioned. That is inevitable.

To do the conversion, there are at least two ways to do, all need coder to do the hard work and no easy support from the library.
[1] One is to use JAXBSource() and JAXBResult() together with Marshaller/Unmarshaller. However, I just make a quick check on the documentation to discover that JAXBSource() actually implicitly use marshal() method for the purpose. That makes early use of Xml annotations resulting in impracticality of this method in converting the object from package items.exec to generated. So this channel is unfortunately a no-go.
[2] The other channel I would think of is to use cloning and casting. But, unfortunately, it seems functionality in that area is till left empty. Figuratively, there is not implementation of some sort of deep-copy of object of one package to another to make the conversion. Hence, that makes the coder the only choice to do it the hard way. This is the demo of how it can be done. For complicated object modelling, it is, I confess, quite too much work to make it attractive.

The approach allows one to massage the concepts in a broad sense, whether it is practical, it is left to each one to evaluate.
Karen Jonathon
Greenhorn

Joined: Jul 25, 2011
Posts: 5
Sorry for the late reply. This is what I was looking for!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification Requested - XML recordset & JAXB classes
 
Similar Threads
org.hibernate.InvalidMappingException: Could not parse mapping document from resource customer.hbm.x
Why is my type unexpected? required variable found value
Doubt on the JAXB: XML Schema to Java mapping for anonymous complex type.
InvalidMappingException
Need Help With JAXB Unmarshaller - Convert XML Back to Java Bean