简体   繁体   中英

JAXB - unmarshall XML message to list

could someone help me how should my Java class look like when I would like to unmarshall this xml?

<content>
        <status>NACK</status>
        <securitiesFinancingTrade>
            <id version="1" type="originalDealId">999999</id>
        </securitiesFinancingTrade>
        <schemaVersion>Schema1</schemaVersion>
        <error>
            <type>MalformedXML</type>
            <message>Message 1</message>
        </error>
        <error>
            <type>MalformedXML</type>
            <message>Message 2</message>
        </error>
    </content>

Currently I have it like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "content", propOrder = {
        "status",
        "securitiesFinancingTrade",
        "schemaVersion",
        "error"
})
public class Content  implements Serializable {

    private final static long serialVersionUID = -1L;
    protected ContentStatusEnumType status;
    protected SecuritiesFinancingTrade securitiesFinancingTrade;
    protected String schemaVersion;
    protected List<Error> error;

    public ContentStatusEnumType getStatus() {
        return status;
    }

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

    public SecuritiesFinancingTrade getSecuritiesFinancingTrade() {
        return securitiesFinancingTrade;
    }

    public void setSecuritiesFinancingTrade(SecuritiesFinancingTrade securitiesFinancingTrade) {
        this.securitiesFinancingTrade = securitiesFinancingTrade;
    }

    public String getSchemaVersion() {
        return schemaVersion;
    }

    public void setSchemaVersion(String schemaVersion) {
        this.schemaVersion = schemaVersion;
    }

    public List<Error> getError() {
        return error;
    }

    public void setError(List<Error> error) {
        this.error = error;
    }
}

and the Error class looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "error", propOrder = {
        "type",
        "message"
})
public class Error  implements Serializable {

    private final static long serialVersionUID = -1L;
    @XmlAttribute(name = "type", required = true)
    protected String type;
    @XmlAttribute(name = "message", required = true)
    protected String message;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

I do not receive any unmarshalling error from JAXB, it even puts two errors in the list as expected, but the type and message there are null.

在此处输入图像描述

In your class Error you have annoated the properties type and message with @XmlAttribute . That means, you can handle XML input with XML attributes , like this:

<error type="MalformedXML" message="Message"></error>

But actually you have input with XML elements , like this:

<error>
    <type>MalformedXML</type>
    <message>Message 1</message>
</error>

So, in your class Error you need to replace @XmlAttribute by @XmlElement , or just omit it altogether.

After doing this, the <error> parts of your XML input will be unmarshalled correctly.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM