users@jaxb.java.net

Re: Problem marshalling part of a xml msg

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 20 Oct 2006 06:57:39 -0700

Frank wrote:
> I have a problem and I don't know how to approach it. Not sure jaxb is
> the solution may have to use DOM. I have a xml msg that has a complex
> element that can accept any thing. I want to pull that out and create
> another xml msg, should be easy right?

Yeah.

> Here is an example schema and document but I get an error:

> [com.sun.istack.SAXException2: unable to marshal type
> "csds.message.ReleaseRequest$Item" as an element because it is missing
> an @XmlRootElement annotation]

Check the javadoc of ddsMsgItem.setAny(). What does it say as "allowed
types"? I suspect you need to add rrI wrapped in a JAXBElement here.

>
>
> code segment that generated that error
> ---------------------------------
> JAXBContext context = JAXBContext.newInstance("csds.message");
> csds.message.ObjectFactory objFact = new
> csds.message.ObjectFactory();
> ReleaseRequest.Item ddsMsgItem =
> objFact.createReleaseRequestItem();
> ReleaseRequest.Item rrI = (ReleaseRequest.Item)
> RelReq.getItem();
> ddsMsgItem.setAny(rrI);
> StringWriter result = new StringWriter();
> Marshaller m =context.createMarshaller();
> m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
> m.marshal( new JAXBElement(new QName("uri","local"),
> ReleaseRequest.Item.class,
> ddsMsgItem), result);
>
> Now if I do the following, no marshal error but the document is not well
> formed, it leaves the name spaces before the now what I want new root
> element.
>
> StringWriter result = new StringWriter();
> Marshaller m = jc.createMarshaller();
> m.marshal( new JAXBElement(new QName("",""),
> ReleaseRequest.Item.class, rrI),
> result);

That is because you are specifying an empty tag name. Specify some
names, like new QName("root").

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com