users@jaxb.java.net

Re: marshalling problem

From: Frank <fgeck_at_optonline.net>
Date: Wed, 15 Nov 2006 13:01:41 -0500

I tried something that Dmitri eluted too to set the item element. I
did the following:

        csds.message.ObjectFactory objFact = new
csds.message.ObjectFactory();
        csds.message.GetDocResponse gdr = objFact.createGetDocResponse();
        csds.message.GetDocResponse.Item item = new
csds.message.GetDocResponse.Item();
        gdr.setTime(MeshUtil.setCurrentTime());
        JAXBElement ele = new JAXBElement(new QName("",""),
String.class, ddsMsg);
        item.setAny(ele);
        gdr.setItem(item);

The weird thing now is if you marshal this xml document out and print it
out all the "<" & ">" signs for the string ele have been replaced with
&lt; and &gt;. Any thoughts? Some encoding issue?

Thanks,

Frank

Frank wrote:

> Yes I created a getDocResponse msg/obj/root element by self using the
> foloowing code.
> String ddsMsg = "some string msg";
> csds.message.ObjectFactory objFact = new
> csds.message.ObjectFactory();
> csds.message.GetDocResponse gdr = objFact.createGetDocResponse();
> csds.message.GetDocResponse.Item item = new
> csds.message.GetDocResponse.Item();
> gdr.setTime(MeshUtil.setCurrentTime());
> item.setAny(ddsMsg);
> gdr.setItem(item);
>
> Thanks I'll see if I can figure that out. I was wondering if I add
> that portion correctly.
>
>
> Dmitri Colebatch wrote:
>
>> Oh sorry - my bad.
>>
>> What is it you want String to be marshalled to? Am I right in
>> thinking that you created the getDocResponse object yourself (as
>> opposed to something you unmarshalled using jaxb).
>>
>> In essence, I think you'll need to create JAXBElement<String>
>> instances to put in your ##any list, although I haven't done this
>> myself - I'm sure others on the list can answer more confidently.
>>
>> cheers
>> dim
>>
>>
>> On 11/15/06, Frank <fgeck_at_optonline.net <mailto:fgeck_at_optonline.net>>
>> wrote:
>>
>> Actually msgObj is a getDocResponse. I wanted to create (maybe
>> incorrectly ) a generic marshaller to a string of a root
>> element/msg.
>>
>>
>> Dmitri Colebatch wrote:
>>
>>> Isn't the problem here that msgObj is a String?
>>>
>>> On 11/15/06, Frank <fgeck_at_optonline.net
>>> <mailto:fgeck_at_optonline.net>> wrote:
>>>
>>> I have a problem with marshaling an object as a complex type
>>> is of any
>>> type (see schema below). I'm trying to just set the element
>>> value to a
>>> string and get the error below.
>>>
>>>
>>> Here is the error:
>>>
>>> 15625 [pool-1-thread-1] FATAL csds.mesh.CsfMeshXrs -
>>> :processGetDocReq,
>>> Problem creating the get doc response
>>> javax.xml.bind.MarshalException
>>> - with linked exception:
>>> [com.sun.istack.SAXException2: unable to marshal type
>>> "java.lang.String"
>>> as an element because it is missing an @XmlRootElement
>>> annotation]
>>> at
>>> com.sun.xml.bind.v2.runtime.MarshallerImpl.write
>>> (MarshallerImpl.java:295)
>>> at
>>> com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:221)
>>> at
>>> javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:77)
>>> at csds.mesh.MeshUtil.createMsgToSend(MeshUtil.java:926)
>>>
>>>
>>> The relevant part of the schema is as follows:
>>> <xsd:element name="getDocResponse">
>>> <xsd:annotation>
>>> <xsd:documentation>
>>> Response from internal MeSH to XRS containing a valid
>>> document.
>>> </xsd:documentation>
>>> </xsd:annotation>
>>> <xsd:complexType>
>>> <xsd:sequence>
>>> <xsd:element name="Time" type="xsd:dateTime"/>
>>> <xsd:element name="Item">
>>> <xsd:complexType>
>>> <xsd:sequence>
>>> <xsd:any namespace="##any"
>>> processContents="strict"/>
>>> </xsd:sequence>
>>> </xsd:complexType>
>>> </xsd:element>
>>> </xsd:sequence>
>>> </xsd:complexType>
>>> </xsd:element>
>>>
>>> Code segment.
>>> Object msgObj; //value passed in
>>>
>>> JAXBContext jc =
>>> JAXBContext.newInstance("csds.message");;
>>> StringWriter result = new StringWriter();
>>> Marshaller m = jc.createMarshaller();
>>> m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
>>> Boolean.TRUE);
>>> m.marshal(msgObj, result); //Fails here
>>> msgStr = result.toString();
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>> <mailto:users-unsubscribe_at_jaxb.dev.java.net>
>>> For additional commands, e-mail:
>>> users-help_at_jaxb.dev.java.net
>>> <mailto:users-help_at_jaxb.dev.java.net>
>>>
>>>
>>