JAXB / MOXy:如何取消/编组包含标量或标量列表的字段

时间:2013-02-14 10:19:50

标签: java jaxb moxy

我有一组属性,单个属性的每个值都是标量(String,Integer,...)或标量集合(Collection,Collection,...)。这是一个XML文档,作为一个例子:

<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>

这是我想要使用的类,但我不知道如何正确标记值字段以使用和生成此结构。它必须以标量或标量列表的形式包含属性元素的已解析内容。数据类型作为属性值存在。

@XmlRootElement(name = "property")
public class Property {

    @XmlAttribute(name = "name")
    protected String name;
    @???
    protected Object value;
}

使用两个字段protected Object scalarprotected List<Object> list,其中一个标记为@XmlValue,另一个标记@XmlElement(name = "value")不起作用。

有人有想法吗?

更新1

我将Property标记如下:

@XmlRootElement(name = "property")
public class Property {

    @XmlAttribute(name = "name")
    protected String name;
    @XmlJavaTypeAdapter(Test2Adapter.class)
    @XmlPath(".")
    protected Object value;
}

我已部分实现了以下Adapter类

public class Test2Adapter extends XmlAdapter<AdaptedValue, Object> {

@Override
public Object unmarshal(AdaptedValue v) throws Exception {
    if (v instanceof Scalar) {
    return ((Scalar) v).value;
    }
    if (v instanceof Complex) {
        return ((Complex) v).value;
    }
    return null;
}

@Override
public AdaptedValue marshal(Object v) throws Exception {
    if (v instanceof String) {
    Scalar s = new Scalar();
    s.value = v;

    return s;
    }
if (v instanceof Collection) {
    Complex c = new Complex();
    c.value = (Collection<? extends Object>) v;

    return c;
}
return null;
}

AdaptedValue:

@XmlSeeAlso({ Scalar.class, Complex.class })
public abstract class AdaptedValue {
}

标量:

public class Scalar extends AdaptedValue {

    @XmlValue
    public Object value;

}

复杂:

public class Complex extends AdaptedValue {

    @XmlAttribute(name = "xsi:type")
    public String type;

    @XmlElement(name = "value")
    public Collection<? extends Object> value   
}

所有内容都正确编组,但解组不起作用。我得到以下异常:

javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-43] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [xs:string] of type[class java.lang.String].
Descriptor:
XMLDescriptor(com.materna.osgi.service.cm.rest.resource.representation.Test2Adapter$AdaptedValue --> [])]
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:190)

1 个答案:

答案 0 :(得分:0)

如果我没错,你需要一个名为XmlJavaTypeAdapter的东西。我很久以前就用过这个,现在我没有方便的代码,但是请看一下这个链接,如果你还是卡住了,请给我一个大喊。

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

在上述帖子的评论部分,“pomcompot”提到使用List / Collection的包装器。我记得使用了包装器类并且它有效。

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