JAXB Marshal用于HashSet,Map和Lists

时间:2016-04-20 17:51:08

标签: java xml spring jaxb

我正在尝试将Java bean转换为xml文档,而我遇到了一些更复杂的接口问题。这是设置:

protected Set<Object> field1;
protected Map<Integer, List<Object>> field2;
protected List<String> field3;
protected List<Object> field4;
protected List<Object> field5;
protected List<Object> field6;
protected List<String> field7;
protected List<Object> field8;

在每个Object(它本身就是一个bean)中,我在每个类的顶部都有以下内容:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "field1",
    "field2",
    "field3",
    "field4",
    "field5",
    "field6",
    "field7",
    "field8"
})
@XmlRootElement(name = "root")

当我整理整个bean时,我一直在获得整数和列表映射的异常。有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:0)

假设你有三个以下的课程

客户类

package comparison;     
import java.util.ArrayList;
import java.util.List;

public class Customer {

    private long id;
    private String name;
    private Address address;
    private List<phonenumber> phoneNumbers;

    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    } 
}

地址类

package comparison;

public class Address {

    private String city;
    private String street;

}

和PhoneNumber类

package comparison;

public class PhoneNumber {

    private String type;
    private String number;

}

现在添加一些虚拟数据

package comparison;

public class Data {

public static Customer CUSTOMER;

static {
    CUSTOMER = new Customer();
    CUSTOMER.setId(123);
    CUSTOMER.setName("Jane Doe");

    Address address = new Address();
    address.setStreet("1 A Street");
    address.setCity("Any Town");
    CUSTOMER.setAddress(address);

    PhoneNumber workPhoneNumber = new PhoneNumber();
    workPhoneNumber.setType("work");
    workPhoneNumber.setNumber("555-WORK");
    CUSTOMER.getPhoneNumbers().add(workPhoneNumber);

    PhoneNumber cellPhoneNumber = new PhoneNumber();
    cellPhoneNumber.setType("cell");
    cellPhoneNumber.setNumber("555-CELL");
    CUSTOMER.getPhoneNumbers().add(cellPhoneNumber);
} 
}

现在您应用编组将对象转换为xml

package comparison.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import comparison.Customer;

import static comparison.Data.CUSTOMER;

public class JAXBDemo {

    public static void main(String[] args) throws Exception {

        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<Customer> jaxbElement = new JAXBElement<Customer>(new QName("customer"), Customer.class, CUSTOMER);
        marshaller.marshal(jaxbElement, System.out);
    }
}
  1. 之前需要在绑定元数据上初始化JAXBContext 可以进行元帅行动。
  2. 与XStream不同,JAXB不格式化 默认情况下是XML,因此我们将启用此功能。
  3. 没有元数据 指定我们需要为JAXB提供根元素名称(和 命名空间)。
  4. 代码将产生结果:

    <customer>
    <id>123</id>
    <address>
        <city>Any Town</city>
        <street>1 A Street</street>
    </address>
    <name>Jane Doe</name>
    <phoneNumbers>
        <number>555-WORK</number>
        <type>work</type>
    </phoneNumbers>
    <phoneNumbers>
        <number>555-CELL</number>
        <type>cell</type>
    </phoneNumbers>
    

    默认情况下,JAXB将访问公共字段和属性。您可以将JAXB配置为使用具有以下包级别注释的字段访问。

    @XmlAccessorType(XmlAccessType.FIELD)
    package comparison;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    

    您可以查看此blogpost了解详情。