使用 JaxB 编组时如何避免添加特定的 element/value

When using JaxB marshalling how to avoid adding a particular element/value

我有一个 XML 数据,如下所示。我需要能够 marshall/unmarshall 这个。 objective 很简单,我在那里没有遇到任何问题

XML 的代码片段

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<messages>
 <message msg-id=\"1\" msg-type=\"ERROR\" msg=\"\"/>
        <message msg-id=\"2\" msg-type=\"INFO\" msg=\"\"/>
        <message msg-id=\"3\" msg-type=\"WARNING\" msg=\"\"/>
</messages>

在我的主 POJO class 消息中,我有一个哈希映射,我用它来将消息映射到 ID。我希望消息映射不进入 XML。 简而言之,我想知道它们是否是任何特定的注释或方式,我可以简单地使用它来避免特定数据(在本例中为 HashMap)在我进行编组时不去生成 XML

编组时生成的 XML 片段

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<messages>
    <message msg-id="1" msg-type="ERROR" msg=" madrid"></message>
    <message msg-id="2" msg-type="INFO" msg="portugal"></message>
    <message msg-id="3" msg-type="WARNING" msg="barcelona"></message>
    <messageMap>
        <entry>
            <key>3</key>
            <value msg-id="3" msg-type="WARNING" msg="barcelona"></value>
        </entry>
        <entry>
            <key>2</key>
            <value msg-id="2" msg-type="INFO" msg="portugal"></value>
        </entry>
        <entry>
            <key>1</key>
            <value msg-id="1" msg-type="ERROR" msg=" madrid"></value>
        </entry>
    </messageMap>
</messages>

在我的主 POJO class 消息中,我有一个哈希映射,我用它来将消息映射到 ID。 我希望消息映射不进入 XML.

===============用于消息输入的POJO===================

package code.rfid.common.salami;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "message")
public class Message {

    @XmlValue
    protected String value;
    @XmlAttribute(name = "msg-id")
    protected String msgId;
    @XmlAttribute(name = "msg-type")
    protected String msgType;
    @XmlAttribute(name = "msg")
    protected String msg;

    public static String INFO = "info";
    public static String ERROR = "error";
    public static String WARNING = "warning";

    public String getValue() {
        return value;
    }

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

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String value) {
        this.msgId = value;
    }

    @Override
    public String toString() {
        return "Message [value=" + value + ", msgId=" + msgId + ", msgType="
                + msgType + ", msg=" + msg + "]";
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String value) {
        this.msgType = value;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String value) {
        this.msg = value;
    }
}

=========消息的POJO====================

package code.rfid.common.salami;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "messages")
public class Messages {

    @Override
    public String toString() {
        return "Messages [message=" + message + ", messageMap=" + messageMap
                + "]";
    }

    protected List<Message> message;
    protected HashMap<String, Message> messageMap;

    public List<Message> getMessage() {
        if (message == null) {
            message = new ArrayList<Message>();
        }
        return this.message;
    }

    public HashMap<String, Message> getMessageMap() {
        if (messageMap == null) {
            messageMap = new HashMap<String, Message>();
        }
        return messageMap;
    }

    public void populateMessageMap()
    {
        messageMap = new HashMap<String, Message>();
        if(message == null || message.isEmpty())
            return;
        for(Message _message: message)
        {
            messageMap.put(_message.getMsgId(), _message);
        }
    }
}

======================== 编组和解组代码

package code.rfid.common;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import code.rfid.common.salami.Messages;

public class MessageJaxBPort {

    static String responseString ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
                            + "<messages>"
                            + "<message msg-id=\"1\" msg-type=\"ERROR\" msg=\" madrid\"/>"
                            + "<message msg-id=\"2\" msg-type=\"INFO\" msg=\"portugal\"/>"
                            + "<message msg-id=\"3\" msg-type=\"WARNING\" msg=\"barcelona\"/>"
                        + "</messages>";

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Messages.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Messages mapmessages = (Messages) jaxbUnmarshaller.unmarshal(new StringReader( responseString));
            System.out.println("UserInfo object :- " + mapmessages.toString());

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(mapmessages, System.out);
        } catch (JAXBException jaxbEx) {
            jaxbEx.printStackTrace();
        }
    }
}

@XmlTransient 注释添加到您的消息映射 属性,如下所示:

@XmlTransient
public HashMap<String, Message> getMessageMap() {

这将导致封送拆收器排除此 属性。