在JAXB中映射XML数组

时间:2013-12-06 10:23:01

标签: xml jaxb

我想使用JAXB映射XML数组。数组对象已创建。但是阵列没有填充。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++

<ns1:intArray xmlns:ns1="http://www.example.org">
    <int>9864</int>
    <int>7291</int>
    <int>6249</int>
    <int>679</int>
</ns1:intArray>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++

@XmlRootElement
public class IntArray {

    private int[] array;

    @XmlElement(name = "int")
    public int[] getArray() {
        return array;
    }

    public void setArray(int[] array) {
        this.array = array;
    }
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++

@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED,
namespace = "http://www.example.org",
xmlns = {@XmlNs(prefix = "ns1",
    namespaceURI = "http://www.example.org")})
package com.xml.test;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++

public static void main(String[] args) throws JAXBException {
    File xml = new File("/media/sda3/array.xml");

    JAXBContext jc = JAXBContext.newInstance(IntArray.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    IntArray obj = (IntArray) unmarshaller.unmarshal(xml);

    int[] arr = obj.getArray();
    System.out.println(arr); // null

}

为什么数组没有填充xml数据?

1 个答案:

答案 0 :(得分:0)

因为您已指定elementFormDefault=XmlNsForm.QUALIFIED,所以int元素应该是名称空间限定的,就像根元素一样。

了解更多信息