inheritance - EclipseLink MOXy: Superclass fields are not set when unmarshaling -
i expect fields of superclass unmarshaled in scenario below, not. make work?
input:
<?xml version="1.0" encoding="utf-8"?> <a>my text</a>
binding:
<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="test"> <java-types> <java-type name="subclass"> <xml-root-element name="a"/> <java-attributes> <xml-element java-attribute="text" xml-path="text()" /> </java-attributes> </java-type> </java-types> </xml-bindings>
classes:
public class superclass { private string text; public string gettext() { return text; } public void settext(string text) { this.text = text; } } public class subclass extends superclass { }
demo:
map<string, object> jaxbcontextproperties = new hashmap<string, object>(1); jaxbcontextproperties.put(jaxbcontextproperties.oxm_metadata_source, "bindings.xml"); jaxbcontext jaxbcontext = jaxbcontextfactory.createcontext(new class[] {superclass.class}, jaxbcontextproperties); unmarshaller unmarshaller = jaxbcontext.createunmarshaller(); superclass superclass = (superclass)unmarshaller.unmarshal(new file("input.xml")); system.out.println(superclass.gettext());
output:
[el warning]: 2013-07-31 15:09:16.602--ignoring attribute [text] on class [subclass] no property generated it. null
the mapping needs made on class belongs to.
option #1 - map superclass
property on subclass
if want map super class properties part of child need mark parent class transient on java-type
element.
bindings.xml
<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum17981925"> <java-types> <java-type name="superclass" xml-transient="true"/> <java-type name="subclass"> <xml-root-element/> <java-attributes> <xml-element java-attribute="text" xml-path="text()" /> </java-attributes> </java-type> </java-types> </xml-bindings>
option #2 - map superclass
property on superclass
alternatively map text
property on superclass
, mapping inherited subclass
.
bindings.xml
<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum17981925"> <java-types> <java-type name="superclass"> <java-attributes> <xml-element java-attribute="text" xml-path="text()" /> </java-attributes> </java-type> <java-type name="subclass"> <xml-root-element/> </java-type> </java-types> </xml-bindings>
demo code
the following demo code can run prove both options work:
input.xml
<subclass>hello world</subclass>
demo
import java.io.file; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.jaxbcontextfactory; import org.eclipse.persistence.jaxb.jaxbcontextproperties; public class demo { public static void main(string[] args) throws exception { map<string, object> jaxbcontextproperties = new hashmap<string, object>(1); jaxbcontextproperties.put(jaxbcontextproperties.oxm_metadata_source, "bindings.xml"); jaxbcontext jaxbcontext = jaxbcontextfactory.createcontext(new class[] {superclass.class}, jaxbcontextproperties); unmarshaller unmarshaller = jaxbcontext.createunmarshaller(); superclass superclass = (superclass)unmarshaller.unmarshal(new file("input.xml")); system.out.println(superclass.gettext()); } }
output
hello world
Comments
Post a Comment