[英]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.我有一个 function 接受 kafka ConsumerRecords<Object, Object> 记录,我需要做的是从每条记录中创建一个 Element object。
Below is how it works for the Json implementation where I create a JsonNode object for each record.下面是 Json 实现的工作原理,我为每条记录创建了一个 JsonNode object。
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().我想做与上面相同的事情,但对于 XML。所以我相信我必须使用 Element 或 Document 而不是 JsonNode,然后使用 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?正如您在上面看到的,我能够简单地使用 JsonFactory 和 Object 映射器将记录转换为 JsonNode,那么 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.您可以像使用“ObjectMapper”一样使用XmlMapper
。 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:这会产生以下 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编辑:为我使用的依赖项添加 Maven 坐标,以防万一
<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());
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.