users@jaxb.java.net

Re: Creating a new XML document from another XML document

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 06 Nov 2006 09:56:28 -0800

Frank wrote:
> Kohsuke,
> The AdvertiseSS is not part of that schema. I do/would like to
> modify some values in that new document but not sure how to determine
> what type of object to create as the root element of AdvertiseSS could
> be 1 of 8 values. I have the schema'(s) for that project thought. But at
> the moment I woudl do the folowing:
>
> ReleaseRequest.Item rrI = (ReleaseRequest.Item) rr.getItem();
>
> so I have the vailue in rrI. Now I have 2 issues. Issue one is that is
> I do the folowing on that:
>
> m.marshal( new JAXBElement(new QName("",""), ReleaseRequest.Item.class,
> rrI), result);
>
> I don't get a well formed message. It is as follows:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?><
> xmlns:ns2="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"><ns3:advertiseSS
> advertisementID="
> .
> .
> .
> </ns3:advertiseSS></>
>
> Need to get ride of the ending "</>" and "<
> xmlns:ns2="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1">"

There must be an object that corresponds to <advertiseSS>. It doesn't
matter if it's yours or someone else's. You should marshal that object,
and not ReleaseRequest.Item.

> And is I want to use JAXB how do I unmarshal a xml document when I don't
> know the root element/what msg it is? The example below is how I've
> seen it done but I know I was going to have a ReleaseRequest msg, how do
> you do it when you don't know the msg type ahead of time?
>
> jc = JAXBContext.newInstance("csds.message");
> Unmarshaller u = jc.createUnmarshaller();
> _rr = (ReleaseRequest) u.unmarshal(new
> ByteArrayInputStream(msgAsString.
> getBytes()));

Typically you'll do instanceof checks.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com