0

I have a XML data which looks like below. I need to be able to marshall/unmarshall this. The objective is simple and I am not facing any problem there

Code snippet for the 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>

In my main POJO class Messages I have a hashmap which I am using for mapping message to the ID. I would like to have the messagemap not go into XML. In short I would like to know if their is any particular annotation or way which I can use simply to avoid a particular data (in this case the HashMap) to not go to generated XML when I do marshalling

Snippet of XML which gets generated on marshalling

<?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>

In my main POJO class Messages I have a hashmap which I am using for mapping message to the ID. I would like to have the messagemap not go into XML.

===============POJO for message entry===================

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 for messages====================

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);
        }
    }
}

======================== Code for marshalling and unmarshalling

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();
        }
    }
}
2
  • 1
    @XmlTransient or am I missing something? Commented Jan 28, 2015 at 19:04
  • actually lexicore is right I simply wanted to know how to avoid a particular value to not go to generated XML on marshalling. Commented Jan 28, 2015 at 19:09

1 Answer 1

3

Add the @XmlTransient annotation to your message map property as follows:

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

This will cause the marshaler to exclude this property.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks was going through the API docs. I was also thinking XmlTransient, was just not sure. I guess I am becoming a bit lazy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.