?
Solved

Convert POJO to a jaxb ObjectFactory

Posted on 2009-05-07
7
Medium Priority
?
2,183 Views
Last Modified: 2013-11-23
How to convert POJO to a jaxb ObjectFactory using marshalling?
0
Comment
Question by:cwatkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24333677
What's your goal here - what do you intend to be your end product?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24333685
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
 
LVL 12

Expert Comment

by:Gibu George
ID: 24334566
0
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!

 

Author Comment

by:cwatkins
ID: 24336526
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
 

Author Comment

by:cwatkins
ID: 24337658
Basically, I want to be able to marshal an object without @XmlRootElement
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24337715
Try generating a schema from the object
0
 

Accepted Solution

by:
cwatkins earned 0 total points
ID: 25228405
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question