Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation
Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation [message #1060223] Thu, 23 May 2013 15:15 Go to next message
Eric Peters is currently offline Eric PetersFriend
Messages: 1
Registered: May 2013
Junior Member
I'm generating a set of classes from an XSD, in this case I'm also using <jaxb:globalBindings localScoping="toplevel"/> to remove the various nesting of the classes

In a simple example XML there is something like:
<PIES>
<Header/>
<Items>
<Item/>
<Item/>
</Items>
</PIES>

After I have a Header class instance I'm trying to marshall it to Xml (marshaller.marshal(o, new StreamResult(sw))) but get the error about the @XmlRootElement not existing:

Caused by: com.sun.istack.SAXException2: unable to marshal type "org.aftermarket.pies.Header" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:249) ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:339) ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494) ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323) ~[jaxb-impl-2.2.6.jar:2.2.6]


Is there a way to specify in a custom bindings file classes that should get the XmlRootElement tag (or whatever else it would need to allow me to Marshall/UnMarshall it separately from the

I really want to avoid modifying the generated java files from xjc. I've seen an example of wrapping the marshalling object with a JAXBElement tag, but that seemed like a hacky solution - and I'm not sure how that would fit in with marshalling objects that already have a proper @XmlRootElement/etc

Thanks,

Eric
Re: Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation [message #1060407 is a reply to message #1060223] Fri, 24 May 2013 13:55 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Hi Eric,

I will demonstrate how you can handle this use case using the following XML Schema. The schema contains 2 named complex types: foo and bar. The foo type has 1 corresponding global element, and the bar type has 2.

XML Schema (schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">
    
    <element name="foo" type="tns:foo"/>
   
    <complexType name="foo">
        <attribute name="a"/>
    </complexType>
    
    <element name="bar1" type="tns:bar"/>
    
    <element name="bar2" type="tns:bar"/>
    
    <complexType name="bar">
        <attribute name="b"/>
    </complexType>

</schema>


Default Generation - xjc schema.xsd

If we generate a Java model from this XML schema we will get Foo and Bar classes and neither of them will be annotated with @XmlRootElement. Instead there will be @XmlElementDecl declarations for each of the global elements in the generated ObjectFactory class.

        ObjectFactory objectFactory = new ObjectFactory();
        
        Foo foo = new Foo();
        JAXBElement<Foo> fooElement = objectFactory.createFoo(foo);
        
        Bar bar = new Bar();
        JAXBElement<Bar> bar1Element = objectFactory.createBar1(bar);
        JAXBElement<Bar> bar2Element = objectFactory.createBar2(bar);


Customized Generation - xjc -extension -b binding.xml schema.xsd

We can leverage an XJC extension to create an @XmlRootElement for a class when there is only 1 global element. For the XML schema in this example an @XmlRootElement will be generated on the Foo class, but not on the Bar class. The binding.xml file looks like:

<?xml version="1.0"?>
<jxb:bindings 
    version="1.0" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
    jxb:extensionBindingPrefixes="xjc" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jxb:globalBindings>
            <xjc:simple/>
        </jxb:globalBindings>
    </jxb:bindings>
</jxb:bindings>


For More Information

- http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html

-Blaise

[Updated on: Fri, 24 May 2013 13:57]

Report message to a moderator

Previous Topic:Call Oracle Function
Next Topic:DML without transaction
Goto Forum:
  


Current Time: Wed Apr 24 14:17:53 GMT 2024

Powered by FUDForum. Page generated in 0.02678 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top