結合XJCとXMLElementWrapper注釈を生成し、カスタマイズ方法
質問
私は、Javaクラスに私のXMLスキーマをコンパイルするJAXBとXJCを使用しています。私は手動で生成されたクラスを編集する必要はありません。私はそのようなXMLスキーマを持っています:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element ref="item" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
XJCはItems
オブジェクトのリストを含むクラスItem
を生成します。クラスItems
を省略し、Item
classに直接Root
オブジェクトのリストを持っている任意のチャンスはありますか?
私は、これは@XMLElementWrapper
アノテーションで行うことができることを知っているが、私はそのような作成するために、XJCを伝える方法がわからない。
任意の提案をありがとう!
敬具、 マルクス
解決
ビャーン・ハンセンは、このの世話をすることができましたXJC用のプラグインを開発しました。残念ながら、元の実装へのリンクが死んでいます。しかし、githubの上のドミトリーKatsuboによるプロジェクトは、いくつかの追加の改善とBjarneの元のコードに基づいて、あります。
→ https://github.com/dmak/jaxb-xew-plugin
<時間>(ジャスト参照用:元のリンク、今死んだ:のhttp:/ /www.conspicio.dk/blog/bjarne/jaxb-xmlelementwrapper-pluginする)
他のヒント
最初は、生成されたいかなる内部クラスが存在しないように、あなたのスキーマを破ることができます
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root" type="Root" />
<xs:complexType name="Root">
<xs:sequence>
<xs:element name="items" type="Items" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Items">
<xs:sequence>
<xs:element name="item" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
あなたはまだだけでなく、すべて一つのファイルに、余分なクラスを取得します。今、あなたは jaxb-xew-plugin
に使用するようにビルドにセクションを追加します。私はMavenを使用するので、私にとっては、これは次のようになります。
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<arg>-no-header</arg>
<arg>-Xxew</arg>
<arg>-Xxew:instantiate lazy</arg>
<arg>-Xxew:delete</arg>
</args>
<plugins>
<plugin>
<groupId>com.github.jaxb-xew-plugin</groupId>
<artifactId>jaxb-xew-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
あなたは、あなたの生成されたクラスは、パッケージ名を持つように名前空間を使用して起動し、それがいけないオブジェクトを削除された場所私は最近、固定バグがありますよう、-Xxew:delete
フラグをオフのままにした場合。また、あなたはgithubのからコードを取得し、1.1-SNAPSHOTとしてそれを使用することができます。
私はあなたが探していると思うことを生成します:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Root", propOrder = {
"items"
})
public class Root {
@XmlElementWrapper(name = "items", required = true)
@XmlElement(name = "item")
protected List<String> items;
public List<String> getItems() {
if (items == null) {
items = new ArrayList<String>();
}
return items;
}
public void setItems(List<String> items) {
this.items = items;
}
}