简体   繁体   中英

Java unmarshalling complex xml into Object

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

and got the object as follows enter image description here

could anyone can mention how I need to create java class correctly to get all the value unmarshall correctly.please note I didn't use package-info.java file here.

This is the code I used for unmarshalling

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

Convert the xml into xsd. After that using eclipse, create jaxb project and point to xsd. java files will be generated based on the xsd file.

Once java schema files are generated, you can use marshall and unmarsahll code.

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; try { unmarshaller = context.createUnmarshaller(); object = unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM