users@jaxb.java.net

Re: Problem marshalling part of a xml msg

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 25 Oct 2006 08:21:47 -0700

Frank wrote:
>> 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.
>
>
> it say's:
> setAny
> public void setAny(java.lang.Object value)
> Sets the value of the any property. Parameters:value - allowed object is
> Object

Thanks. I guess I need to improve this javadoc so that it will be bit
more helpful.

>
>>
>>
>>>
>>>
>>> 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").
>
> But I want the root element of my xml document to be the element next in
> the ReleaseRequest.Item i.e. advertiseSS

Then you need to get the object that corresponds to your "advertiseSS"
element and marshal that object.

>
> from the document of:
> <?xml version="1.0" encoding="UTF-8"?>
> <ns1:releaseRequest xmlns="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"
> xmlns:ism="urn:us:gov:ic:ism:v2"
> xmlns:ns1="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="urn:us:gov:dod:army:cerdec:jbfsa:csds:r1
> C:\data\JBFSA\r1\csds_src\config\csds_msgs.xsd">
> <ns1:Time>2001-12-17T09:30:47.0Z</ns1:Time>
> <ns1:Item>
> <ns2:advertiseSS
> advertisementID="1b094a9d-063b-409b-8a3b-923061a5c983"
> commandDateTime="2006-05-25T09:49:56.593-04:00" userID="peer"
> xmlns="http://mitre.org/DDS" xmlns:ns2="http://mitre.org/DDS">
> ..
>
> But I will not know what that element tag is (reason for any). So how
> do I create a new object where the contents between my item are a new
> document and the next element is the root of that xml document?
>
> ie. new document:
> <?xml version="1.0" encoding="UTF-8"?>
> <ns2:advertiseSS advertisementID="1b094a9d-063b-409b-8a3b-923061a5c983"
> commandDateTime="2006-05-25T09:49:56.593-04:00" userID="peer"
> xmlns="http://mitre.org/DDS" xmlns:ns2="http://mitre.org/DDS">....
> </ns2:advertiseSS>
>
>
>>
>>
>


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com