使用JAXB检索嵌套的XML元素值

时间:2018-09-04 08:02:03

标签: java xml jaxb

对于JAXB还是一个新手,我试图从以下XML的元素中检索值。

问题是,无论我做什么,所有类字段都为null  尝试

<?xml version="1.0" encoding="ISO-8859-1"?>
<euloack>
    <header></header>
    <data>
        <combination_filename>
            combination_filename
        </combination_filename>
        <checksum_filename>
            checksum_filename
        </checksum_filename>
        <ssc_xml_filename>
            ssc_xml_filename
        </ssc_xml_filename>
        <acknowledgement>
            acknowledgement
        </acknowledgement>
    </data>
</euloack>

所以我正在使用下面的类:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "euloack")
public class ACKMessageVo {

    private String combinationFileName;
    private String checksumFile;
    private String sscXmlFilename;
    private String acknowledgement;

    public ACKMessageVo() {

    }

    public ACKMessageVo(String combinationFileName, String checksumFile, String sscXmlFilename,
            String acknowledgement) {
        super();
        this.combinationFileName = combinationFileName;
        this.checksumFile = checksumFile;
        this.sscXmlFilename = sscXmlFilename;
        this.acknowledgement = acknowledgement;
    }

    public String getCombinationFileName() {
        return combinationFileName;
    }

    @XmlElement(name = "combination_filename")
    public void setCombinationFileName(String combinationFileName) {
        this.combinationFileName = combinationFileName;
    }

    public String getChecksumFile() {
        return checksumFile;
    }

    @XmlElement(name = "checksum_filename")
    public void setChecksumFile(String checksumFile) {
        this.checksumFile = checksumFile;
    }

    public String getSscXmlFilename() {
        return sscXmlFilename;
    }

    @XmlElement(name = "ssc_xml_filename")
    public void setSscXmlFilename(String sscXmlFilename) {
        this.sscXmlFilename = sscXmlFilename;
    }

    public String getAcknowledgement() {
        return acknowledgement;
    }

    @XmlElement(name = "acknowledgement")
    public void setAcknowledgement(String acknowledgement) {
        this.acknowledgement = acknowledgement;
    }

    @Override
    public String toString() {
        return combinationFileName + "," + checksumFile + "," + sscXmlFilename + "," + acknowledgement;

    }

}

解组官代码:

JAXBContext jaxbContext = JAXBContext.newInstance(ACKMessageVo.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ACKMessageVo ackMessage = (ACKMessageVo) jaxbUnmarshaller.unmarshal(inputStream);

3 个答案:

答案 0 :(得分:1)

问题是在“ euloack”节点下有一个“数据”节点,然后是您指定的其他4个。

答案 1 :(得分:1)

您需要修复ACKMessageVo类,使其具有数据字段。更改班级,使其看起来像这样:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "header",
        "data"
})
@XmlRootElement(name = "ACKMessageVo")
public class ACKMessageVo {

    @XmlElement(required = true)
    private String header;
    @XmlElement(required = true)
    private ACKMessageVo.Data data;

    public String getHeader() {
        return header;
    }

    public void setHeader(String value) {
        this.header = value;
    }

    public ACKMessageVo.Data getData() {
        return data;
    }

    public void setData(ACKMessageVo.Data value) {
        this.data = value;
    }


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
            "combinationFilename",
            "checksumFilename",
            "sscXmlFilename",
            "acknowledgement"
    })
    public static class Data {

        @XmlElement(name = "combination_filename", required = true)
        private String combinationFilename;
        @XmlElement(name = "checksum_filename", required = true)
        private String checksumFilename;
        @XmlElement(name = "ssc_xml_filename", required = true)
        private String sscXmlFilename;
        @XmlElement(required = true)
        private String acknowledgement;

        public String getCombinationFilename() {
            return combinationFilename;
        }

        public void setCombinationFilename(String value) {
            this.combinationFilename = value;
        }

        public String getChecksumFilename() {
            return checksumFilename;
        }

        public void setChecksumFilename(String value) {
            this.checksumFilename = value;
        }

        public String getSscXmlFilename() {
            return sscXmlFilename;
        }

        public void setSscXmlFilename(String value) {
            this.sscXmlFilename = value;
        }

        public String getAcknowledgement() {
            return acknowledgement;
        }

        public void setAcknowledgement(String value) {
            this.acknowledgement = value;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "combinationFilename='" + combinationFilename + '\'' +
                    ", checksumFilename='" + checksumFilename + '\'' +
                    ", sscXmlFilename='" + sscXmlFilename + '\'' +
                    ", acknowledgement='" + acknowledgement + '\'' +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "ACKMessageVo{" +
                "header='" + header + '\'' +
                ", data=" + data +
                '}';
    }
}

答案 2 :(得分:0)

结束了以下操作...

ACKMessageVO

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "euloack")
public class ACKMessageVo {

    private String header;

    private ACKMessageVoData data;

    public ACKMessageVo() {

    }

    public ACKMessageVo(ACKMessageVoData data) {
        super();
        setData(data);
    }

    public ACKMessageVo(String combinationFileName, String checksumFile, String sscXmlFilename, String acknowledgement) {
        super();
        setData(new ACKMessageVoData(combinationFileName, checksumFile, sscXmlFilename, acknowledgement));
    }

    public String getHeader() {
        return header;
    }

    @XmlElement
    public void setHeader(String header) {
        this.header = header;
    }

    public ACKMessageVoData getData() {
        return data;
    }

    @XmlElement
    public void setData(ACKMessageVoData data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return data.toString();
    }

}

ACKMessageVoData

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * Data for the ACK
 *
 */
@XmlRootElement(name = "data")
@XmlType(propOrder = { "combinationFileName", "checksumFile", "sscXmlFilename", "acknowledgement" })
public class ACKMessageVoData {

    private String combinationFileName;
    private String checksumFile;
    private String sscXmlFilename;
    private String acknowledgement;

    public ACKMessageVoData() {

    }

    public ACKMessageVoData(String combinationFileName, String checksumFile, String sscXmlFilename, String acknowledgement) {
        super();
        this.combinationFileName = combinationFileName;
        this.checksumFile = checksumFile;
        this.sscXmlFilename = sscXmlFilename;
        this.acknowledgement = acknowledgement;
    }

    public String getCombinationFileName() {
        return combinationFileName;
    }

    @XmlElement(name = "combination_filename")
    public void setCombinationFileName(String combinationFileName) {
        this.combinationFileName = combinationFileName;
    }

    public String getChecksumFile() {
        return checksumFile;
    }

    @XmlElement(name = "checksum_filename")
    public void setChecksumFile(String checksumFile) {
        this.checksumFile = checksumFile;
    }

    public String getSscXmlFilename() {
        return sscXmlFilename;
    }

    @XmlElement(name = "ssc_xml_filename")
    public void setSscXmlFilename(String sscXmlFilename) {
        this.sscXmlFilename = sscXmlFilename;
    }

    public String getAcknowledgement() {
        return acknowledgement;
    }

    @XmlElement(name = "acknowledgement")
    public void setAcknowledgement(String acknowledgement) {
        this.acknowledgement = acknowledgement;
    }

    @Override
    public String toString() {
        return combinationFileName.trim() + "," + checksumFile.trim() + "," + sscXmlFilename.trim() + "," + acknowledgement.trim();
    }

}