如何解组包装的xml

时间:2014-10-14 15:31:02

标签: java xml jaxb

我的xml文件如下所示,我尝试解组它。

<Projects 
xmlns="http://ws.wso2.org/dataservice">
<Project>
    <projectId 
        xmlns="http://ws.wso2.org/dataservice">1
    </projectId>
    <projectName 
        xmlns="http://ws.wso2.org/dataservice">project1
    </projectName>
    <startDate 
        xmlns="http://ws.wso2.org/dataservice">2014-09-09T00:00:00.000-07:00
    </startDate>
    <endDate 
        xmlns="http://ws.wso2.org/dataservice">2015-09-09T00:00:00.000-07:00
    </endDate>
    <projectManagerUserId 
        xmlns="http://ws.wso2.org/dataservice">3167
    </projectManagerUserId>
    <statusId 
        xmlns="http://ws.wso2.org/dataservice">2
    </statusId>
    <teamId 
        xmlns="http://ws.wso2.org/dataservice">2
    </teamId>
</Project>
</Projects>

我的bean类如下所示,

@XmlAccessorType(XmlAccessType.FIELD)
public class Project {
    @XmlAttribute
    private String projectId;
    @XmlAttribute
    private String projectName;
    @XmlAttribute
    private String startDate;
    @XmlAttribute
    private String endDate;
    @XmlAttribute
    private String projectManager;
    @XmlAttribute
    private String status;
    @XmlAttribute
    private String teamId;

public Project() {
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getProjectManager() {
    return projectManager;
}

public void setProjectManager(String projectManager) {
    this.projectManager = projectManager;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getTeamId() {
    return teamId;
}

public void setTeamId(String teamId) {
    this.teamId = teamId;
}

public String getProjectId() {
    return projectId;
}

public void setProjectId(String projectId) {
    this.projectId = projectId;
}
}

我的另一只豆,

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProjectCollection {

    @XmlElement(name="Project")
    private List<Project> projects;

    public ProjectCollection() {
    }

    public List<Project> getProjects() {
        return projects;
    }

    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }
}

但这并不像预期的那样有效。有人能指出这段代码有什么问题。?

1 个答案:

答案 0 :(得分:4)

您需要解决一些问题:

指定根元素

默认情况下,JAXB会认为根元素是projectCollection,您可以将其更改为与XML匹配,如下所示:

@XmlRootElement(name="Projects") 
@XmlAccessorType(XmlAccessType.FIELD)
public class ProjectCollection {

映射命名空间限定

由于您的XML文档是名称空间限定的,因此您需要映射它。这可以使用包级别@XmlSchema注释来完成。更改下面片段中的包以匹配您自己的包。注意:下面的确切内容位于名为package-info.java的源文件中,其中的确切内容如下所示,包名称与您的模型名称相匹配。

@XmlSchema( 
    namespace = "http://ws.wso2.org/dataservice", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package org.example;

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

我在博客上写了更多关于JAXB和名称空间限定的内容:

@XmlAttribute注释更改为@XmlElement

您有很多字段注释@XmlAttribute,但在XML中它们是元素。您可以进行此更改,也可以完全取消@XmlElement注释,因为它是默认值。