1

次の 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;

    // ...
}

IdAttributeString ( ) が 1 つしか含まれていないためid、これらの String を my に直接マップして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 (ここから):

<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では、バインディング ファイルを使用してを追加する正しい方法は何ですか?

4

1 に答える 1

0

あなたがやろうとしていることは間違っています。xml 型 IdAttribute を Java 型 String に完全に置き換える必要があります。

一般的な dom.Element タイプで作業し、<-> 文字列に変換する必要があります。例を次に示します。ケースの任意の XMLの JAXB @XmlAdapter は、Java タイプ Bar を String に置き換えます。

他の解決策は、生成されたクラスに動作を追加して、メソッドを FoobarType に追加して、String のリストがあるかのように動作するようにすることです。
たとえば、メソッドを追加します。
List<String> getIds()
void setIds(List<String>)

これを行う方法を説明するリンクは次のとおりです。「動作の追加」

于 2015-08-26T09:47:17.500 に答える