JAXB XmlAdapter通过绑定文件

时间:2015-08-16 18:50:16

标签: jaxb jaxb2 xjc maven-jaxb2-plugin xjb

考虑一下这个XSD:

<xsd:schema targetNamespace="http://foobar" xmlns:tns="http://foobar"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:complexType name="IdAttribute">
        <xsd:attribute name="id" type="xsd:token" />
    </xsd:complexType>

    <xsd:complexType name="FoobarType">
        <xsd:sequence>
            <xsd:element name="someIds" type="tns:IdAttribute" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="Foobar" type="tns:FoobarType" />
</xsd:schema>

导致以下生成的Java类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FoobarType", propOrder = {"someIds"})
public class FoobarType {

    @XmlElement(required = true)
    protected List<IdAttribute> someIds;

    // ...
}

由于IdAttribute只包含一个字符串(id),因此我想将这些字符串直接映射到我的FoobarType以便于使用。因此我写了XmlAdapter

public class IdAttributeAdapter extends XmlAdapter<IdAttribute, String> { ... }

我已手动编辑生成的类,以验证我的XmlAdapter是否按预期工作(确实如此):

@XmlElement(required = true)
@XmlJavaTypeAdapter(IdAttributeAdapter.class)
protected List<String> someIds;

当然我希望在代码生成过程中完成这个小代码更改,所以我添加了以下绑定文件(src/main/xjb/bindings.xjb):

尝试1:

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">

    <jaxb:bindings schemaLocation="../resources/xsd/foobar.xsd">
        <jaxb:bindings node="//xsd:complexType[@name='IdAttribute']">
            <xjc:javaType
                name="java.lang.String"
                adapter="org.foobar.IdAttributeAdapter" />
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

结果

com.sun.istack.SAXParseException2; systemId: file:/C:/dev/foobar/src/main/xjb/bindings.xjb;
lineNumber: 12; columnNumber: 91;
compiler was unable to honor this conversion customization. It is attached to a wrong place, or its inconsistent with other bindings.

尝试2(来自here):

<jaxb:globalBindings xmlns:foo="http://foobar" xsi:schemaLocation="http://foobar ../resources/xsd/foobar.xsd">
    <xjc:javaType
        name="java.lang.String"
        xmlType="foo:IdAttribute"
        adapter="org.foobar.IdAttributeAdapter" />
</jaxb:globalBindings>

结果

com.sun.istack.SAXParseException2; systemId: file:/C:/dev/projects/luna/oasch/src/main/xjb/bindings.xjb;
lineNumber: 8; columnNumber: 112;
undefined simple type "{http://foobar}IdAttribute".

我尝试了一些变体,但它们都导致了类似的错误。那么使用绑定文件添加XmlApender的正确方法是什么?

另请参阅GitHub上的示例项目。

1 个答案:

答案 0 :(得分:0)

你尝试做的是错的。 您应该使用java类型String完全替换xml类型IdAttribute。

您必须使用通用dom.Element类型并将其转换为&lt; - &gt;串。 这是一个例子:JAXB @XmlAdapter for arbitrary XML为您的情况用String替换java类型的Bar。

其他解决方案是向生成的类添加行为,以便向FoobarType添加方法,就像它具有字符串列表一样工作 例如添加方法
List<String> getIds()
void setIds(List<String>)

以下链接说明了如何执行此操作:"Adding behavior"

相关问题