• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2196
  • Last Modified:

Convert POJO to a jaxb ObjectFactory

How to convert POJO to a jaxb ObjectFactory using marshalling?
0
cwatkins
Asked:
cwatkins
  • 3
  • 3
1 Solution
 
CEHJCommented:
What's your goal here - what do you intend to be your end product?
0
 
CEHJCommented:
If, as i think, you want to end up with xml, see

http://www.onjava.com/pub/a/onjava/2004/12/15/jaxb.html
0
 
Gibu GeorgeCTOCommented:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cwatkinsAuthor Commented:
I have an application that returns a POJO.  
There are different POJO structures.  Meaning, a POJO could be an object, a list of objects, map, or an object that is comprised of other objects (e.g., 1-1 or 1-n relationship).
I want to be able to convert the POJO to XML via jaxb Marshaller.
Afterwards, I want to be able to unmarshal the generated XML for a different application.

Example:
 
public void generateXml(List<pojoObj> myPojoObj) throws JAXBException {
 5    
 9     // Get a JAXB Context for the object we created above
10     JAXBContext context = JAXBContext.newInstance(pojoObj.getClass());
11
12     // To convert ex to XML
13     Marshaller marshaller = context.createMarshaller();
14
15     // Format the resulting XML
16     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
17     StringWriter sw = new StringWriter();
18
19     // marshall the object to XML
20     marshaller.marshal(myPojoObj, sw);
21
22     // print it out for this example
23     System.out.println(sw.toString());
24   }
 
When I execute the  code snippet, the following exception occurs:
 
unable to marshal type "java.util.ArrayList"
as an element because it is missing an @XMLRootElement annotation

Open in new window

0
 
cwatkinsAuthor Commented:
Basically, I want to be able to marshal an object without @XmlRootElement
0
 
CEHJCommented:
Try generating a schema from the object
0
 
cwatkinsAuthor Commented:
There are POJO that can easily be marshall/unmarshal using the POJO corresponding XML.  However, there are arbitrary objects in which there is no associated POJO.  Therefore,  I was able to use XMLEncoder create archives of any Java objects.  
Referene: http://java.sun.com/products/jfc/tsc/articles/persistence4/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now