我有一个XML数据,如下所示。我需要能够对此进行编组/解组。目标很简单,我没有遇到任何问题
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类消息中,我有一个哈希映射,我用它来将消息映射到ID。我想让messagemap不要进入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类消息中,我有一个哈希映射,我用它来将消息映射到ID。 我想让messagemap不要进入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();
}
}
}
答案 0 :(得分:3)
将@XmlTransient注释添加到您的邮件地图属性中,如下所示:
@XmlTransient
public HashMap<String, Message> getMessageMap() {
这将导致封送程序排除此属性。