users@jaxb.java.net

Re: marshalling jaxb content to an xsd:anyType schema element

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 5 Sep 2008 08:43:20 +0200

XML data coming as an instance of xsd:anyType is unmarshalled into an object
of org.w3c.dom.Element.

To marshal, create an object of a class implementing Element. JAXB uses
apache.xerces.internal.dom.ElementNSImpl re-packaged into com.sun.org.

You can easily verify this by writing an XML file containing
<Bob><foo>...<bar>...</Bob>
and unmarshalling it. Analyse the object returned from Bob's parent with
getBob().
This object (or any other org.w3c.dom.Element) can be set into Bob's parent,
and it'll
marshal.

kr
Wolfgang


On Fri, Aug 29, 2008 at 10:40 AM, Trevor Paterson <
trevor.paterson_at_roslin.ed.ac.uk> wrote:

>
> 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.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>