将JPA对象转换为XML / JAXB

时间:2012-06-01 15:46:25

标签: java xml spring hibernate jaxb

我想将数据从表格转换为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,但这是一对一的映射关系(即XML结构中没有子元素或内部元素)。我不明白如何执行子元素和示例xml文件所需的条件规则。

我在Spring 3中使用JPA / Hibernate。是否可以生成示例xml文件,还是需要手动执行(使用for循环和if语句),因为XML文件的格式?

由于

2 个答案:

答案 0 :(得分:2)

看看Dozer。假设您有一个JPA实体类和一个JAXB类。 Dozer是一个在两个这样的bean之间进行映射的框架。但是,您需要指定应将哪个属性映射到哪里。

答案 1 :(得分:2)

我一般认为从你正在做的事情中走另一条路更容易。也就是说,使用XJC生成JAXB对象,然后使用orm.xml文件对数据库执行JPA映射。您可以使用标准XJC插件和JAXB配置文件自定义生成的Java对象。

这种方法的优点是只维护一组Java类(可能有一些JAXB模型的补充子类),并且它还倾向于提升更稳定的XML契约。

出于某种原因,人们倾向于小心使用单独的XML文件进行ORM。但是,有些情况是合理的,我认为这是其中一种情况。