Java 使用JAXB解组XML后无法获取正确的值

Java 使用JAXB解组XML后无法获取正确的值,java,xml,jaxb,Java,Xml,Jaxb,我有下面的XML文件,当我使用下面的JAVA代码解压下面的XML时 我没有得到案例经理标签值。 我使用innovation.getCasemanager()获取,得到的值为空。 事实上,这应该是回报dhocksta@umich.edu“价值 我实际上遇到了这个问题,因为案例管理器标记包含XML属性。如果它不包含XML属性,那么我可以获得案例管理器的正确值 XML文件: <innovation file_number="0269"> <title>3D Static Str

我有下面的XML文件,当我使用下面的JAVA代码解压下面的XML时

我没有得到案例经理标签值。 我使用innovation.getCasemanager()获取,得到的值为空。 事实上,这应该是回报dhocksta@umich.edu“价值

我实际上遇到了这个问题,因为案例管理器标记包含XML属性。如果它不包含XML属性,那么我可以获得案例管理器的正确值

XML文件:

<innovation file_number="0269">
<title>3D Static Strength Prediction Program</title>
<brief_description>3D Static Strength Prediction Program</brief_description>
<categories/>
<tags>
  <tag>Healthcare</tag>
  <tag>Manufacturing</tag>
  <tag>_Other</tag>
</tags>
<full_description>test</full_description>
<case_manager first_name="Doug" last_name="Hockstad">dhocksta@umich.edu</case_manager>
<status>Published</status>
</innovation>
Innovations.java

package com.test.innovation.validation;
@XmlRootElement(name = "innovations")
public class Innovations {

private String organization;
private List<Innovation> innovationList = new ArrayList<Innovation>();

/**
 * @return the organization
 */
@XmlElement(name = "organization")
public String getOrganization() {
    return organization;
}

/**
 * @param organization
 *            the organization to set
 */
public void setOrganization(String organization) {
    this.organization = organization;
}

/**
 * @return the innovationList
 */
@XmlElement(name = "innovation")
public List<Innovation> getInnovationList() {
    return innovationList;
}

/**
 * @param innovationList
 *            the innovationList to set
 */
public void setInnovationList(List<Innovation> innovationList) {
    this.innovationList = innovationList;
}

public void printRecord() {
    int i = 0;
    for (Innovation innovation : innovationList) {
        i++;
        String str = "";
        System.out.println("-----------------------------------------------------------------------");
        str = "\nFileNumber : " + innovation.getFileNumber();
        str += "\nTitle : " + innovation.getTitle();
        str += "\nBriefDescription : " + innovation.getBriefDescription();

        if (innovation.getCategories() != null) {
            if (innovation.getCategories().getCategoryList() != null) {
                str += "\nCategories : " + 
                                        innovation.getCategories().getCategoryList().toString();
            }
        }

        if (innovation.getTags() != null) {
            if (innovation.getTags().getTagList() != null) {
                str += "\nTags : " + innovation.getTags().getTagList().toString();
            }
        }

        str += "\nFullDescription : " + innovation.getFullDescription();
        str += "\nCaseManager : " + innovation.getCaseManager();
        str += "\nFirstName : " + innovation.getCaseManager().getFirstName();
        str += "\nLastName : " + innovation.getCaseManager().getLastName();
        str += "\nStatus : " + innovation.getStatus();

        if (innovation.getPatentNumber() != null) {
            str += "\nPatentNumberInformation : " + innovation.getPatentNumber().toString();
        }
        if (innovation.getPatentStatus() != null) {
            str += "\nPatentStatusInformation : " + innovation.getPatentStatus().toString();
        }
        str += "\nOrganizationName : " + getOrganization();
        System.out.println("Record Number " + i + " :: " + str);
        System.out.println("");
    }
}

}

我认为在“CaseManager”类中声明带有@XmlValue注释的变量将对您有所帮助。
有关更多信息,请查看这些链接。
1.

2.

@Prabhaker…非常感谢!!
package com.test.innovation.validation;
@XmlRootElement(name = "innovations")
public class Innovations {

private String organization;
private List<Innovation> innovationList = new ArrayList<Innovation>();

/**
 * @return the organization
 */
@XmlElement(name = "organization")
public String getOrganization() {
    return organization;
}

/**
 * @param organization
 *            the organization to set
 */
public void setOrganization(String organization) {
    this.organization = organization;
}

/**
 * @return the innovationList
 */
@XmlElement(name = "innovation")
public List<Innovation> getInnovationList() {
    return innovationList;
}

/**
 * @param innovationList
 *            the innovationList to set
 */
public void setInnovationList(List<Innovation> innovationList) {
    this.innovationList = innovationList;
}

public void printRecord() {
    int i = 0;
    for (Innovation innovation : innovationList) {
        i++;
        String str = "";
        System.out.println("-----------------------------------------------------------------------");
        str = "\nFileNumber : " + innovation.getFileNumber();
        str += "\nTitle : " + innovation.getTitle();
        str += "\nBriefDescription : " + innovation.getBriefDescription();

        if (innovation.getCategories() != null) {
            if (innovation.getCategories().getCategoryList() != null) {
                str += "\nCategories : " + 
                                        innovation.getCategories().getCategoryList().toString();
            }
        }

        if (innovation.getTags() != null) {
            if (innovation.getTags().getTagList() != null) {
                str += "\nTags : " + innovation.getTags().getTagList().toString();
            }
        }

        str += "\nFullDescription : " + innovation.getFullDescription();
        str += "\nCaseManager : " + innovation.getCaseManager();
        str += "\nFirstName : " + innovation.getCaseManager().getFirstName();
        str += "\nLastName : " + innovation.getCaseManager().getLastName();
        str += "\nStatus : " + innovation.getStatus();

        if (innovation.getPatentNumber() != null) {
            str += "\nPatentNumberInformation : " + innovation.getPatentNumber().toString();
        }
        if (innovation.getPatentStatus() != null) {
            str += "\nPatentStatusInformation : " + innovation.getPatentStatus().toString();
        }
        str += "\nOrganizationName : " + getOrganization();
        System.out.println("Record Number " + i + " :: " + str);
        System.out.println("");
    }
}

}
package com.test.innovation.validation;
@XmlType
public class Innovation {

private String fileNumber;
private String title;
private String briefDescription;
private String fullDescription;
private List<String> patentNumber = new ArrayList<String>();
private List<String> patentStatus = new ArrayList<String>();
private CaseManager caseManager;
private Categories categories;
private Tags tags;
private String status;

/**
 * @return the fileNumber
 */
@XmlAttribute(name = "file_number")
public String getFileNumber() {
    return fileNumber;
}

/**
 * @param fileNumber
 *            the fileNumber to set
 */
public void setFileNumber(String fileNumber) {
    this.fileNumber = fileNumber;
}

/**
 * @return the title
 */
@XmlElement(name = "title")
public String getTitle() {
    return title;
}

/**
 * @param title
 *            the title to set
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @return the briefDescription
 */
@XmlElement(name = "brief_description")
public String getBriefDescription() {
    return briefDescription;
}

/**
 * @param briefDescription
 *            the briefDescription to set
 */
public void setBriefDescription(String briefDescription) {
    this.briefDescription = briefDescription;
}

/**
 * @return the fullDescription
 */
@XmlElement(name = "full_description")
public String getFullDescription() {
    return fullDescription;
}

/**
 * @param fullDescription
 *            the fullDescription to set
 */
public void setFullDescription(String fullDescription) {
    this.fullDescription = fullDescription;
}

/**
 * @return the caseManager
 */
@XmlElement(name = "case_manager")
public CaseManager getCaseManager() {
    return caseManager;
}

/**
 * @param caseManager
 *            the caseManager to set
 */
public void setCaseManager(CaseManager caseManager) {
    this.caseManager = caseManager;
}

/**
 * @return the status
 */
@XmlElement(name = "status")
public String getStatus() {
    return status;
}

/**
 * @param status
 *            the status to set
 */
public void setStatus(String status) {
    this.status = status;
}

/**
 * @return the categories
 */
@XmlElement(name = "categories")
public Categories getCategories() {
    return categories;
}

/**
 * @param categories
 *            the categories to set
 */
public void setCategories(Categories categories) {
    this.categories = categories;
}

/**
 * @return the tags
 */
@XmlElement(name = "tags")
public Tags getTags() {
    return tags;
}

/**
 * @param tags
 *            the tags to set
 */
public void setTags(Tags tags) {
    this.tags = tags;
}

/**
 * @return the patentNumber
 */
@XmlElement(name = "patent_number")
public List<String> getPatentNumber() {
    return patentNumber;
}

/**
 * @param patentNumber
 *            the patentNumber to set
 */
public void setPatentNumber(List<String> patentNumber) {
    this.patentNumber = patentNumber;
}

/**
 * @return the patentStatus
 */
@XmlElement(name = "patent_status")
public List<String> getPatentStatus() {
    return patentStatus;
}

/**
 * @param patentStatus
 *            the patentStatus to set
 */
public void setPatentStatus(List<String> patentStatus) {
    this.patentStatus = patentStatus;
}

}
package com.test.innovation.validation;

import javax.xml.bind.annotation.XmlAttribute;

public class CaseManager {

private String firstName;
private String lastName;

/**
 * @return the firstName
 */
@XmlAttribute(name = "first_name")
public String getFirstName() {
    return firstName;
}

/**
 * @param firstName
 *            the firstName to set
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

/**
 * @return the lastName
 */
@XmlAttribute(name = "last_name")
public String getLastName() {
    return lastName;
}

/**
 * @param lastName
 *            the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}

}