Java 使用JAXB创建Springbean和必要的属性

Java 使用JAXB创建Springbean和必要的属性,java,jaxb,Java,Jaxb,我在定制JAXB Marshaller时遇到问题。我有我的封送员代码: public void marshaller(AddressMap addMap, File file) { try { JAXBContext jaxbContext = JAXBContext.newInstance(AddressMap.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

我在定制JAXB 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>

印第安纳州
39
-85.0
美国
惠斯曼路
8540-8704
47001
相反,我需要它看起来像这样:

<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>

注意:我是专家组的负责人和成员

您可以使用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 {

}
地址映射

@xmlsdescriminatorvalue
注释用于指定描述符节点上与实例类相关的值。在这个类中,我们还使用
@XmlPath
注释根据
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/Output


了解更多信息


添加您的
AddressMap
类的定义。这是什么意思?添加名称空间?还是注释?JAXB将带注释的POJO绑定到XML。在本例中,POJO是您的
AddressMap
类。我们需要看看您是如何定义它的,以便能够帮助您。所以,请在您的问题中包含其中的相关代码部分。非常感谢!这是非常有帮助的。