I tried to unmarshall following xml file.
<ns2:Triggermessage xmlns="http://www.asd.com/messaging" xmlns:ns2="http://www.asd.com/messaging/closing/trigger">
<Header>
<creationTimeStamp>2017-05-23T12:11:46.950+00:00</creationTimeStamp>
<businessDate>2017-05-23</businessDate>
<messageId>20170523-22:30:00_Calypso Rpt 7_444_0</messageId>
<conversationId>7397dbd3-5177-40ff-acb5-1b2ba8d0d1f6</conversationId>
<numberOfRecords>1</numberOfRecords>
<sentBy>ABCDE</sentBy>
<sendto>Camplico</sendto>
<processingCycle>ACD</processingCycle>
</Header>
<ns2:Request>
<ns2:ProcessingCircle>EOD</ns2:ProcessingCircle>
</ns2:Request>
</ns2:Triggermessage>
Here I created classes in this way.(not all the classes are mentioned here)
**TriggerMessage.java**
@XmlRootElement(name = "TriggerMessage", namespace="http://www.asd.com/messaging/closing/trigger")
public class TriggerMessage {
private Header header;
private Request request;
@XmlElement(name = "Header")
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
@XmlElement(name = "Request")
public Request getRequest() {
return request;
}
public void setRequest(Request request) {
this.request = request;
}
}
**Header.java**
private CreationTimeStamp creationTimeStamp;
private BusinessDate businessDate;
private MessageId messageId;
private ConversationId conversationId;
private SentBy sentBy;
private SendTo sendTo;
private ProcessingCycle processingCycle;
private NumberOfRecords numberOfRecords;
@XmlElement(name = "creationTimeStamp")
public CreationTimeStamp getCreationTimeStamp() {
return creationTimeStamp;
}
public void setCreationTimeStamp(CreationTimeStamp creationTimeStamp) {
this.creationTimeStamp = creationTimeStamp;
}
@XmlElement(name = "businessDate")
public BusinessDate getBusinessDate() {
return businessDate;
}
public void setBusinessDate(BusinessDate businessDate) {
this.businessDate = businessDate;
}
@XmlElement(name = "conversationId")
public ConversationId getConversationId() {
return conversationId;
}
public void setConversationId(ConversationId conversationId) {
this.conversationId = conversationId;
}
@XmlElement(name = "messageId")
public MessageId getMessageId() {
return messageId;
}
public void setMessageId(MessageId messageId) {
this.messageId = messageId;
}
-
-
-}
**CreationTimeStamp.java**
public class CreationTimeStamp {
private String value;
@XmlMixed
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
**Request.java**
@XmlType(namespace="http://www.asd.com/messaging/closing/trigger")
public class Request {
private ProcessingCircle processingCircle;
@XmlElement(name = "processingCircle")
public ProcessingCircle getProcessingCircle() {
return processingCircle;
}
public void setProcessingCircle(ProcessingCircle processingCircle) {
this.processingCircle = processingCircle;
}
}
并获得如下对象enter image description here
任何人都可以提到我需要如何正确创建java类才能正确地取消所有值。请注意我这里没有使用package-info.java文件。
这是我用于解组的代码
private List<Object> unMarshalMessages(List<String> messages) {
List<Object> messageObjects = new ArrayList<Object>();
JAXBContext context = null;
try {
context = JAXBContext.newInstance(Class.forName("com.msgs.calpico.TriggerMessage"));
Unmarshaller m = context.createUnmarshaller();
StringReader reader = null;
for (String message : messages) {
reader = new StringReader(message);
messageObjects.add(((com.msgs.calpico.TriggerMessage) m.unmarshal(reader)));
}
} catch (JAXBException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
LOGGER.info("After unmarshal the output messages size: " + messageObjects.size());
return messageObjects;
}
答案 0 :(得分:0)
将xml转换为xsd。 之后使用eclipse,创建jaxb项目并指向xsd。将根据xsd文件生成java文件。
生成java模式文件后,您可以使用marshall和unmarsahll代码。
private static JAXBContext _jaxbContext;
static
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(Connection.class.getClassLoader()); //NOPMD.
StringBuffer sb = new StringBuffer();
sb.append("com.microsoft.schemas.windowsazurerm"); //schema package name
_jaxbContext = JAXBContext.newInstance(sb.toString());
}
catch (Exception e)
{
logger.error(e);
}
finally
{
Thread.currentThread().setContextClassLoader(cl);
}
}
Unmarshaller unmarshaller; Object object = null; 尝试 { unmarshaller = context.createUnmarshaller(); object = unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); }