Reputation: 1727
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();
}
}
}
Upvotes: 0
Views: 1507
Reputation: 12527
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.
Upvotes: 3