Convert POJO to a jaxb ObjectFactory

How to convert POJO to a jaxb ObjectFactory using marshalling?
Who is Participating?
cwatkinsConnect With a Mentor Author 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.  
What's your goal here - what do you intend to be your end product?
If, as i think, you want to end up with xml, see
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Gibu GeorgeChief Technology OfficerCommented:
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.

public void generateXml(List<pojoObj> myPojoObj) throws JAXBException {
 9     // Get a JAXB Context for the object we created above
10     JAXBContext context = JAXBContext.newInstance(pojoObj.getClass());
12     // To convert ex to XML
13     Marshaller marshaller = context.createMarshaller();
15     // Format the resulting XML
16     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
17     StringWriter sw = new StringWriter();
19     // marshall the object to XML
20     marshaller.marshal(myPojoObj, sw);
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

cwatkinsAuthor Commented:
Basically, I want to be able to marshal an object without @XmlRootElement
Try generating a schema from the object
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.