'Adding back the nodes in xml using Java JAXB
The following is my xml:
<pickingOrderBeginEventMessage xmlns="http://www.xmlns.walmartstores.com/SuppyChain/FulfillmentManagement">
<hdr:MessageHeader>
</hdr:MessageHeader>
<MessageBody>
<orderBeginEvent>
<node>
</node>
<fulfillmentOrders>
<fulfillmentOrder>
<orderNbr>1</orderNbr>
<priorityCode>1</priorityCode>
<assignedPickUserId>alpha</assignedPickUserId>
<userId>123</userId>
</fulfillmentOrder>
</fulfillmentOrders>
</orderBeginEvent>
<MessageExtensions />
</MessageBody>
</pickingOrderBeginEventMessage>
I want to remove the value <orderBeginEvent> and add it back later.I can achieve the first part where I am deleting but cant set it back to the way it was in the xml. It is throwing me a nullpointerexception.
This is my POJO class.
public class MessageBody implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name = "RoutingInfo")
RoutingInfoPickBgn routingInfo;
@XmlElement(name = "orderBeginEvent")
@Accessors(fluent = true)
OrderBeginEvent orderBeginEvent;
}
This is how I am marshaling it using JAXB:
public String updateXML() {
try {
JAXBContext context = JAXBContext.newInstance(PickingOrderBeginEventMessage.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
File updateXml = new File("src/test/resources/xml/updatePickBgn.xml");
mar.marshal(pickBegin, updateXml);
String absolutePath = updateXml.getAbsolutePath();
String updatedXml = readFromResources(absolutePath);
return updatedXml;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
I am able to remove orderBeginEvent by setting it to null but I am not sure how to add it back.
public void checkNoOrderBeginEvent() {
FulfillmentOrder orderNbr = pickBegin.getMessageBody().setOrderBeginEvent(null);
String updatedXml = updateXML();
//This code removed the OrderBeginEvent
}
The objective of the code is to after deleting the orderBeginEvent i need to put it back the way it was. I can achieve the former which is to delete the orderBeginEvent but dont know how to set it back. Any help will be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
