I am working on a component for an enterprise message queue that consumes a message from a queue and sends it as a text message. The message is picked up by my consumer in JSON format (I do not have the ability to modify how the message is received).
{
"id":"19ADFASDFGG456SDGE",
"body":"Hello World!",
"to":[15189511011, 19178567788]
}
Once it is received by my consumer I need to populate an XML file like this
<Request>
<Identification>
<UserID>CONSTANT_USER</UserID>
<Password>CONSTANT_PASS</Password>
</Identification>
<Service>
<ServiceName>SendMessage</ServiceName>
<ServiceDetail>
<CombiMessage>
<CombiList>
<Individual type='sms'>${to}</Individual>
</CombiList>
<Text>${body}</Text>
</CombiMessage>
</ServiceDetail>
</Service>
</Request>
Where ${to}
and ${body}
are populated by by the JSON object. That XML cannot be generated in the code (it needs to reside in a resource folder as an xml file). In addition to consuming the message and subsequently sending a text message, the consumer also has logging capabilities and it passes the JSON object (or a POJO containing that data) off to another system.
Basically when a JSON object is consumed off the queue right now I am using a library called Jackson to populate a Pojo that looks like this.
public class Message {
private Set<PhoneNumber> to;
private String body;
private String id;
}
And the JSON object populates that Pojo via Jackson like this.
public static Message getMessage(String json) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, Message.class);
}
From there I now have a POJO that is an exact copy of the JSON object and I can send it off to the logging system, to the other system, and also I can populate the XML with it.
However I haven't found an ideal way to populate the XML. Currently I have something simple like this
private static String populate(String request, Message message) {
return request.replaceAll("${to}", message.getTo().toString()).replaceAll("${body}", message.getBody());
}
This is fine in this regard but only because I oversimplified for the sake of asking this question. Sometimes the XML file has nearly a thousand fields that need to be populated. Can anyone recommend a solution for this last step? How to do this dynamically? Basically I would like to map fields in my POJO with nodes in the xml file (also remembering that I need to keep that xml file external).