java - JAXB won't unmarshal my previously marshalled interface impls -


here working marshalling code:

for starters have entity class following:

@xmlrootelement @xmlaccessortype(xmlaccesstype.none) public final class entity implements {     private map<string, component> components = new hashmap<string, component>();      @xmlelementwrapper(name="components")     @xmlanyelement     public list<component> getcomponentlist() {         return new arraylist<component>(components.values());     }      public void setcomponentlist(list<component> comps) {         for(component c : comps) {             components.put(c.getcomponenttype(), c);         }     }      ... } 

component varied interface implementations nice , round beans annotated.

now, marshalling produces looking xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <entity>     <components>        <containercomponent>            <children>                <entity>                    <components>                        <containercomponent>                            <children>                         <entity>                             <components>                                <directioncomponent>                                   <direction x="0.0" y="1.0"/>                                </directioncomponent>                                <sizecomponent>                                   <size x="52.0" y="12.0"/>                                </sizecomponent>                                ... 

as can see, each component type has own tag name doesn't seem enough jaxb because

exception in thread "main" java.lang.classcastexception: com.sun.org.apache.xerces.internal.dom.elementnsimpl cannot cast tobacco.core.components.component     @ tobacco.core.components.entity.setcomponentlist(entity.java:121)     @ tobacco.core.components.entity$jaxbaccessorm_getcomponentlist_setcomponentlist_java_util_list.set(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.reflect.lister$collectionlister.endpacking(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.reflect.lister$collectionlister.endpacking(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.scope.finish(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingcontext.endscope(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.property.arrayerproperty$itemsloader.leaveelement(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingcontext.endelement(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.saxconnector.endelement(unknown source)     @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.endelement(unknown source)     @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scanendelement(unknown source)     @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl$fragmentcontentdriver.next(unknown source)     @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl.next(unknown source)     @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.next(unknown source)     @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scandocument(unknown source)     @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(unknown source)     @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(unknown source)     @ com.sun.org.apache.xerces.internal.parsers.xmlparser.parse(unknown source)     @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.parse(unknown source)     @ com.sun.org.apache.xerces.internal.jaxp.saxparserimpl$jaxpsaxparser.parse(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unknown source)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal(unknown source)     @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(unknown source)     @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(unknown source)     @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(unknown source)     @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(unknown source)     @ tobacco.core.serialization.xmlentityconverter.toentity(xmlentityconverter.java:32)     @ tobacco.core.loader.xmlloader.loadentitytree(xmlloader.java:43)     @ tobacco.game.test.main.main.main(main.java:75) 

at other end of process receive elementnsimpl objects.

i though using objectfactory after reading , testing i'm convinced it's no use: couldn't find way access tag name on creator method, , if i'm not sure jaxb accept object , fill properties happily or if have fill them myself, absolutely impractical.

i think this guy had same problem. work out of box moxy? there way without switching implementations?

you can use @xmlelements annotation instead of @xmlanyelement , list possible subclasses in example:

@xmlelementwrapper(name = "components") @xmlelements(    {       @xmlelement(name = "directioncomponent", type = directioncomponent.class),       @xmlelement(name = "sizecomponent", type = sizecomponent.class),    }) public list<component> getcomponentlist()  {     return new arraylist<component>(components.values()); }       

also, in case don't need use @xmlrootelement annotation on directioncomponent , sizecomponent classes.

ps. also, can use list of components instead of map

@xmlrootelement @xmlaccessortype(xmlaccesstype.none) public final class entity   {     private list<component> components = new arraylist<component>();      @xmlelementwrapper(name = "components")     @xmlelements(        {           @xmlelement(name = "directioncomponent", type = directioncomponent.class),           @xmlelement(name = "sizecomponent", type = sizecomponent.class),        })     public list<component> getcomponentlist()      {         return components;     }      public void setcomponentlist(list<component> comps)      {          this.components = comps;     }     //... } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -