I have a function that takes in kafka ConsumerRecords<Object, Object> records and what I need to do is make an Element object out of each record.
Below is how it works for the Json implementation where I create a JsonNode object for each record.
for (ConsumerRecord<Object, Object> record: records) {
KafkaMessage kafkaMsgVal = (KafkaMessage) record.value();
String rawJson = kafkaMsgVal.getMsgValue();
JsonNode jsonNode = null;
try {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
jsonNode = mapper.readTree(rawJson);
}
}
I would like to do the same thing as above but for XML. So instead of JsonNode, I believe I have to use Element, or Document and then use the document.getRootElement(). What is the best way to do this? As you can see above, I was able to simply use JsonFactory and Object mapper to convert the record into a JsonNode, what would be the equivalent for XML?
Below is the idea for what I'm going for:
for (ConsumerRecord<Object, Object> record: records) {
KafkaMessage kafkaMsgVal = (KafkaMessage) record.value();
String rawXml = kafkaMsgVal.getMsgValue();
Element element = null;
try {
//What to do here to convert the rawXml into an Element or Document object?
}
}
You can use
XmlMapper
pretty much the same way you have been using `ObjectMapper. Here is a sample code that I tested with:This produces the below output:
EDIT: Adding the Maven co-ordinates for the dependency that I used, just in case
To get a 'Document' from the string you can do something like this: