テーブルのデータを XML ファイルに変換したいと考えています。データは次の形式です
Continent Country County Town
--------------------------------------------------------------
Africa Kenya South Mombasa
Africa Egypt South Cairo
Europe England North Birmingham
Asia Japan South Tokyo
生成される結果の XML ドキュメントは、次のようなものになります。
<continents>
<continent name="Africa">
<countries>
<country>
<countryName>Kenya</countryName>
<county>South</county>
<town>Mombasa</town>
</country>
<country>
<countryName>Egypt</countryName>
<county>South</county>
<town>Cairo</town>
</country>
</countries>
</continent>
<continent name="Europe">
<countries>
<country>
<countryName>England</countryName>
<county>North</county>
<town>Birminghan</town>
</country>
</countries>
</continent>
<continent name="Asia">
<countries>
<country>
<countryName>Japan</countryName>
<county>South</county>
<town>Tokyo</town>
</country>
</countries>
</continent>
</continents>
私がやろうとしているのは、データベースから JPA オブジェクトにデータを読み込むことです。これは、次のオブジェクトのリストを返します
@Entity
@Indexed
@Table(name="TOWN")
public class Town {
protected String continent;
protected String country;
protected String county;
protected String town;
@Id
@Column(name="CONTINENT")
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
@Column(name="COUNTRY")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(name="COUNTY")
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
@Column(name="TOWN")
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
}
データベースから行を取得する呼び出しを実行すると、次の形式でリストが取得されます。
List<Town> towns = entityManager.getAllTowns();
XML ファイルを生成できるようにするには、リストを反復処理して、それぞれの "Town" オブジェクトを処理する必要があります。私は DOM を使用できますが、多くの for ループと if ステートメントが XML ドキュメントを生成できるようになると感じています。リストを反復処理して上記の xml ファイルを作成することなく、XML ドキュメントを生成することは可能ですか?
JPA オブジェクトを Jaxb に変換しましたが、これは 1 対 1 のマッピングによるフラットな関係でした (つまり、XML 構造に子要素や内部要素はありません)。サンプル xml ファイルに必要な子要素と条件付きルールの実行方法がわかりません。
Spring 3 で JPA/Hibernate を使用しています。サンプル xml ファイルを生成することは可能ですか、それとも XML ファイルの形式のために手動で (for ループと if ステートメントを使用して) 行う必要がありますか?
ありがとう