java - EclipseLink MOXy stores instances of DOM Element for fields of type Object annotated with @XmlElement -
we using moxy jaxb in our project.
model class:
@xmlrootelement(name = "field") @xmltype(proporder = {"id","value"}) public class fielddata{ @xmlattribute private string id; @xmlattribute private object value; public string getid() { homecoming id; } public void setid(string id) { this.id = id; } public object getvalue() { homecoming value; } public void setvalue(object value) { this.value = value; } } for utilize case, want value of type object may primitive info type value here. take them strings initially. 1 time object, type conversion , save same field. above utilize case working fine. when alter @xmlattribute @xmlelement not working. see value unmarshalled instance of elementnsimpl. there work around this?
here brain dump on seeing:
demo codei utilize same demo code different mappings described below:
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(foo.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("input.xml"); foo foo = (foo) unmarshaller.unmarshal(xml); system.out.println(foo.getbar().getclass()); } } use case #1 - object property mapped @xmlattribute java model foo
we utilize @xmlattribute annotation map property @xmlattribute. note: isn't valid configuration when using jaxb reference implementation.
import javax.xml.bind.annotation.*; @xmlrootelement public class foo { private object bar; @xmlattribute public object getbar() { homecoming bar; } public void setbar(object bar) { this.bar = bar; } } xml #1 input.xml
in xml document below bar attribute contains numeric digits.
<?xml version="1.0" encoding="utf-8"?> <foo bar="123"/> output
as there no typing info (in xml or in java class), moxy brings value in string. string concrete type can represent possible values on xml attribute.
class java.lang.string xml #2 input.xml
in xml document below bar attribute contains alphabet characters.
<?xml version="1.0" encoding="utf-8"?> <foo bar="hello world"/> output
a string concrete type can represent possible values on xml attribute.
class java.lang.string use case #2 object property mapped @xmlelement java model foo
in version of foo class not annotate bar property, same annotating @xmlelement.
import javax.xml.bind.annotation.*; @xmlrootelement public class foo { private object bar; public object getbar() { homecoming bar; } public void setbar(object bar) { this.bar = bar; } } xml #1 (simple element) input.xml
<?xml version="1.0" encoding="utf-8"?> <foo> <bar>hello world</bar> </foo> output
class com.sun.org.apache.xerces.internal.dom.elementnsimpl xml #2 (complex element) input.xml
instead of containing test, bar element contains xml attributes , kid elements.
<?xml version="1.0" encoding="utf-8"?> <foo> <bar a="1"> <b>2</b> <c>3</c> </bar> </foo> output
now start see why jaxb treats value dom, element arbitrarily complex dom element becomes construction can hold possible value.
class com.sun.org.apache.xerces.internal.dom.elementnsimpl xml #3 (typed element) input.xml
in xml document below
<?xml version="1.0" encoding="utf-8"?> <foo xmlns:xsd="http://www.w3.org/2001/xmlschema"> <bar xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:type="xsd:string">hello world</bar> </foo> output
class java.lang.string use case #3 object property mapped @xmlelement(type=string) java model foo
in version of foo class annotate bar property @xmlelement(type=string.class). far java concerned property still of typeobject, jaxb treat property if it's typestring`.
import javax.xml.bind.annotation.*; @xmlrootelement public class foo { private object bar; @xmlelement(type=string) public object getbar() { homecoming bar; } public void setbar(object bar) { this.bar = bar; } } xml #1 (simple element) now see value of bar element treated string.
input.xml
<?xml version="1.0" encoding="utf-8"?> <foo> <bar>hello world</bar> </foo> output
class java.lang.string java jaxb eclipselink moxy
No comments:
Post a Comment