简体   繁体   中英

Mapping ConsumerRecord to Element - What are JsonFactory and ObjectMapper equivalent for XML?

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:

public static void main(String[] args) {
    XmlMapper xmlMapper = new XmlMapper();
    
    String myXML = "<cookies>" + 
                        "<cookie><type>Chocolate</type><price>5.0</price></cookie> " + 
                        "<cookie><type>Sugar</type><price>5.0</price></cookie>" + 
                   "</cookies>";  
    try {
        // Note that the readTree method returns a `JsonNode` result
        JsonNode root = xmlMapper.readTree(myXML);
        System.out.println(root);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This produces the below output:

{"cookie":[{"type":"Chocolate","price":"5.0"},{"type":"Sugar","price":"5.0"}]}

EDIT: Adding the Maven co-ordinates for the dependency that I used, just in case

<dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
   <version>2.14.1</version>
</dependency>

To get a 'Document' from the string you can do something like this:

...
    StringReader strReader = new StringReader(myXML);
    InputSource inSource = new InputSource(strReader);
        
    // Get a DocumentBuilder instance
    DocumentBuilder docBuilder = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
        
    // Parse String to Document
    Document doc = docBuilder.parse(inSource);
        
        
    // Printing the 'document' element name
    System.out.println(doc.getDocumentElement().getTagName());

...

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