使用JAXB创建spring bean和必要的属性

时间:2012-12-14 19:06:09

标签: java jaxb

我无法自定义JAXB Marshaller。我有我的marshaller代码:

public void marshaller(AddressMap addMap, File file) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(AddressMap.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(addMap, System.out);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

输出如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ObjectMap>
 <Prop> Indiana</Prop>
 <Prop1>39.0</Prop1>
 <Prop2>-85.0</Prop2>
 <Prop3> United States</Prop3>
 <Prop4> Hueseman Rd</Prop4>
 <Prop5> 8540-8704</Prop5>
 <Prop6> 47001</Prop6>
</ObjectMap>

相反,我需要它看起来像这样:

<bean class="classname">
    <property name="PropName" value="object value" />
    <property name="PropName1" value="object value" />
    <property name="PropName2" value="object value" />
    <property name="PropName3" value="object value" />
    <property name="PropName4" value="object value" />
    <property name="PropName5" value="object value" />
    <property name="PropName6" value="object value" />
</bean>

1 个答案:

答案 0 :(得分:2)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

您可以使用MOXy的@XmlDesciminatorNode@XmlPath扩展来映射此用例。下面是一个基于我假设您的对象模型的示例。

<强> ObjectMap的

@XmlDescriminatorNode注释允许您指定希望将特定XML属性用作继承指示符。

package forum13884782;

import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlRootElement(name="bean")
@XmlDiscriminatorNode("@class")
public class ObjectMap {

}

<强> AddressMap

@XmlDescriminatorValue注释用于指定与实例类相关的描述符节点上的值。在本课程中,我们还使用@XmlPath注释来指示我们希望根据其property属性的值映射到哪个name元素。

package forum13884782;

import javax.xml.bind.annotation.*;

import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement(name="bean")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("AddressMap")
public class AddressMap extends ObjectMap {

    @XmlPath("property[@name='PropName']/@value")
    String prop;

    @XmlPath("property[@name='PropName1']/@value")
    String prop1;

    @XmlPath("property[@name='PropName2']/@value")
    String prop2;

    @XmlPath("property[@name='PropName3']/@value")
    String prop3;

    @XmlPath("property[@name='PropName4']/@value")
    String prop4;

    @XmlPath("property[@name='PropName5']/@value")
    String prop5;

    @XmlPath("property[@name='PropName6']/@value")
    String prop6;

}

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的程序包中包含名为jaxb.properties的文件,并带有以下条目。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

<强>演示

以下演示代码将XML消息转换为AddressMap的实例,然后再转换回XML。

package forum13884782;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(AddressMap.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13884782/input.xml");
        AddressMap addressMap = (AddressMap) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(addressMap, System.out);
    }

}

<强> input.xml中/输出

<bean class="AddressMap">
    <property name="PropName" value="Indiana" />
    <property name="PropName1" value="39.0" />
    <property name="PropName2" value="-85.0" />
    <property name="PropName3" value="United States" />
    <property name="PropName4" value="Hueseman Rd" />
    <property name="PropName5" value="8540-8704" />
    <property name="PropName6" value="47001" />
</bean>

了解更多信息