users@jaxb.java.net

marshalling jaxb content to an xsd:anyType schema element

From: Trevor Paterson <trevor.paterson_at_roslin.ed.ac.uk>
Date: Fri, 29 Aug 2008 01:40:48 -0700 (PDT)

I am having problems marshalling jaxb data to an xml document, where an
element in the bound schema is defined as xsd:anyType (which is represented
in the jaxb java generated from the schema as of type Object).

i.e. I need to include in the data for this node both text content and
another element

i.e the schema is
<xsd:element name="Bob" type="xsd:anyType">

and the data I want to add is

“some text”<ns:newElement/>

“some text” works ok, as the marshaller can map this to a String – but
however I try to add additional elements to the data, the marshaller thows
an error when trying to serialize it

adding a JAXBElement seems to be the correct way to go (?)

code example:
//xrel is the jaxb object to which I want to add the information

Element e1 = (Element) doc.createElementNS(myNamespace, “Bob");
Element e2 = (Element) doc.createElement(type);
e2.setAttribute("xmlns:gdr", anotherNamespace);
e2.setTextContent(“some text”);
e1.appendChild(e2);
        
         QName qname = new QName(myNamespace, "Bob", "gmd");

         JAXBElement element = new JAXBElement(qname, Element.class, e1);

and when I try to marshall the whole data to an xml document

it throws:
Instance of "javax.xml.bind.JAXBElement" is substituting "java.lang.Object",
but "javax.xml.bind.JAXBElement" is bound to an anonymous type.

So how do I do this properly to allow the marshaller to serialise this
complex content where the schema stipulates ‘anyType’?

-- 
View this message in context: http://www.nabble.com/marshalling-jaxb-content-to-an-xsd%3AanyType-schema-element-tp19216004p19216004.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.