通过绑定文件的JAXB XmlAdapter

通过绑定文件的JAXB XmlAdapter,jaxb,xjc,maven-jaxb2-plugin,xjb,Jaxb,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:

<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>
我已经手动编辑了生成的类,以验证我的
XmlAdapter
是否按预期工作(确实如此):

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

您尝试的方法是错误的。 您应该用java类型字符串完全替换xml类型IdAttribute

您必须使用泛型dom.Element类型并将其转换为字符串。 下面是一个示例:对于您的情况,将java类型栏替换为字符串

另一种解决方案是将行为添加到生成的类中,以便将方法添加到FoobarType中,使其工作起来就像它有一个字符串列表一样
e、 g.添加方法
列出getid()

void setid(列表)


这里有一个链接解释了如何做到这一点:

你试图做的是错误的。 您应该用java类型字符串完全替换xml类型IdAttribute

您必须使用泛型dom.Element类型并将其转换为字符串。 下面是一个示例:对于您的情况,将java类型栏替换为字符串

另一种解决方案是将行为添加到生成的类中,以便将方法添加到FoobarType中,使其工作起来就像它有一个字符串列表一样
e、 g.添加方法
列出getid()

void setid(列表)


这里有一个链接解释了如何做到这一点:

你找到了怎么做吗?你找到了怎么做吗?
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FoobarType", propOrder = {"someIds"})
public class FoobarType {

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

    // ...
}
public class IdAttributeAdapter extends XmlAdapter<IdAttribute, String> { ... }
@XmlElement(required = true)
@XmlJavaTypeAdapter(IdAttributeAdapter.class)
protected List<String> someIds;
<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.
<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".