jaxb嵌套列表元素返回null

时间:2018-03-21 12:19:35

标签: java jaxb

我正在尝试使用以下jaxb代码将xml转换为java对象。 当我解组xml文件时,我只获取Settings元素中的version属性的值。连接值为空。 请帮我找出我的错误。我可以添加更多细节吗? PS:请原谅我在java中的初级知识。

XML文件:

id  low_Rank    Rank
1   0           1
2   1           1
3   2           1
4   0           2
5   1           2
6   2           2
7   3           2
8   0           3
9   1           3

POJO课程:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<Settings version="3" xmlns="urn:Adapter-v3">
    <Connections>
    <Connection name ="Dev" description="DEV">
      <Share version="CS" siteURL="https://example.com" />
    </Connection>    
    <Connection name ="Dev1" description="DEV1">
      <Share version="OM" siteURL="https://example.com" /> 
    </Connection>
</Connections>
</Settings>

主要文件

@XmlRootElement(name="Settings", namespace="urn:Adapter-v3")
public class Settings {

    Connections connections;
    private String version;

    @XmlElement(name="Connections")
    public Connections getConnections() {
        return connections;
    }

    public void setConnections(Connections connections) {
        this.connections = connections;
    }
    @XmlAttribute(name="version")
    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

}
@XmlRootElement(name="Connections ")
public class Connections {
    List<Connection> connection;
@XmlElement(name="Connection")  
public List<Connection> getConnection() {
        return connection;
    }
    public void setConnection(List<Connection> connection) {
        this.connection = connection;
    }
}
@XmlRootElement(name="Connection")
public class Connection {
    private String name;
    private String description;
    Share share;
@XmlAttribute(name="name")
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @XmlAttribute(name="description")
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        @XmlElement(name="Share")
        public Share getShare() {
            return share;
        }
        public void setShare(Share share) {
            this.share = share;
        }
}
@XmlRootElement(name="Share")
public class Share {
private String version;
private String siteUrl;

@XmlAttribute(name="version")
public String getVersion() {
    return version;
}
public void setVersion(String version) {
    this.version = version;
}
@XmlAttribute(name="siteURL")
public String getSiteUrl() {
    return siteUrl;
}
public void setSiteUrl(String siteUrl) {
    this.siteUrl = siteUrl;
}

}

1 个答案:

答案 0 :(得分:1)

您已为namespace指定了@XmlRootElement,但未为其他元素指定。 @XmlElement(name="Connections")将属性映射为Connections元素,不带名称空间。

namespace="urn:Adapter-v3"添加到所有其他元素,或者更好地将package-info.java添加到包中:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "urn:Adapter-v3",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.acme.foo;