Tuesday, 15 April 2014

java - JAXB/MOXy: How to un-/marshall a field containing a scalar or list of scalars -



java - JAXB/MOXy: How to un-/marshall a field containing a scalar or list of scalars -

i have set of properties , each value of single property either scalar (string, integer, ...) or collection of scalars (collection, collection, ...). here xml document serving as example:

<properties xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:java="http://java.oracle.com" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <property name="test#1" xsi:type="xs:int">1</property> <property name="test#2" xsi:type="java:int[]"> <value xsi:type="xs:int">1</value> <value xsi:type="xs:int">2</value> <value xsi:type="xs:int">3</value> <value xsi:type="xs:int">4</value> </property> </properties>

this class use, have no thought how tag value field correctly consume , produce structure. must contain parsed content of property element in form of scalar or list of scalars. datatype nowadays attribute value.

@xmlrootelement(name = "property") public class property { @xmlattribute(name = "name") protected string name; @??? protected object value; }

using 2 fields protected object scalar , protected list<object> list, 1 tagged @xmlvalue, other @xmlelement(name = "value") not working.

has idea?

update 1

i tagged property follows:

@xmlrootelement(name = "property") public class property { @xmlattribute(name = "name") protected string name; @xmljavatypeadapter(test2adapter.class) @xmlpath(".") protected object value; }

i have partly implemented next adapter class

public class test2adapter extends xmladapter<adaptedvalue, object> { @override public object unmarshal(adaptedvalue v) throws exception { if (v instanceof scalar) { homecoming ((scalar) v).value; } if (v instanceof complex) { homecoming ((complex) v).value; } homecoming null; } @override public adaptedvalue marshal(object v) throws exception { if (v instanceof string) { scalar s = new scalar(); s.value = v; homecoming s; } if (v instanceof collection) { complex c = new complex(); c.value = (collection<? extends object>) v; homecoming c; } homecoming null; }

adaptedvalue:

@xmlseealso({ scalar.class, complex.class }) public abstract class adaptedvalue { }

scalar:

public class scalar extends adaptedvalue { @xmlvalue public object value; }

complex :

public class complex extends adaptedvalue { @xmlattribute(name = "xsi:type") public string type; @xmlelement(name = "value") public collection<? extends object> value }

everything marshalled correctly, unmarshalling not work. next exception:

javax.xml.bind.unmarshalexception - linked exception: [exception [eclipselink-43] (eclipse persistence services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.descriptorexception exception description: missing class indicator field value [xs:string] of type[class java.lang.string]. descriptor: xmldescriptor(com.materna.osgi.service.cm.rest.resource.representation.test2adapter$adaptedvalue --> [])] @ org.eclipse.persistence.jaxb.jaxbunmarshaller.unmarshal(jaxbunmarshaller.java:190)

if not wrong, need called xmljavatypeadapter. used long time ago , not have code handy right now, please take @ link , if still stuck give me shout.

http://weblogs.java.net/blog/kohsuke/archive/2005/04/xmladapter_in_j.html

in comments section of above post, "pomcompot" mentions using wrapper list/collection. remember using wrapper class , worked.

http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp#xmljavatypeadapter

java jaxb moxy

No comments:

Post a Comment