users@jaxb.java.net

Re: Problem marshalling part of a xml msg

From: Frank <fgeck_at_optonline.net>
Date: Fri, 20 Oct 2006 21:18:20 -0400

Kohsuke Kawaguchi wrote:

> 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.


it say's:
setAny
public void setAny(java.lang.Object value)
Sets the value of the any property. Parameters:value - allowed object is
Object

>
>
>>
>>
>> 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

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>


>
>