• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Clarification Requested - XML recordset & JAXB classes

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
>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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late reply. This is what I was looking for!
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic