使用Jackson将XML属性添加到手动构建的节点树

使用Jackson将XML属性添加到手动构建的节点树,第1张

使用Jackson将XML属性添加到手动构建的节点树

attribute
由于
ObjectNode
对序列化一无所知,因此无法标记给定的属性。您可以对
POJO
类执行此 *** 作,并且
com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
仅在
@JacksonXmlProperty(isAttribute= true)
给定属性使用批注时才进行处理。我建议为
POJO
需要属性的元素创建元素,并使用
Jackson

XML
批注或实现
JsonSerializable
接口。它可能如下所示:

import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.JsonSerializable;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.SerializerProvider;import com.fasterxml.jackson.databind.jsontype.TypeSerializer;import com.fasterxml.jackson.databind.node.ObjectNode;import com.fasterxml.jackson.dataformat.xml.XmlMapper;import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;import java.io.IOException;import java.util.linkedHashMap;import java.util.Map;public class XmlMapperApp {    public static void main(String[] args) throws Exception {        Map<String, Object> map = new linkedHashMap<>();        map.put("Puppy", Boolean.TRUE);        map.put("Apple", 2);        map.put("Jet", "Li");        Examples examples = new Examples();        examples.setOverlyComplicated("yes");        examples.setMap(map);        XmlMapper mapper = new XmlMapper();        mapper.enable(SerializationFeature.INDENT_OUTPUT);        ObjectNode rootNode = mapper.createObjectNode();        rootNode.putPOJO("Examples", examples);        ObjectNode currentNode = rootNode.putObject("Single");        currentNode.put("One", 1);        mapper.writevalue(System.out, rootNode);    }}class Examples implements JsonSerializable {    @Override    public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {        ToXmlGenerator toXmlGenerator = (ToXmlGenerator) gen;        toXmlGenerator.writeStartObject();        writeAttributes(toXmlGenerator);        writeMap(toXmlGenerator);        toXmlGenerator.writeEndObject();    }    private void writeAttributes(ToXmlGenerator gen) throws IOException {        if (overlyComplicated != null) { gen.setNextIsAttribute(true); gen.writeFieldName("overlyComplicated"); gen.writeString(overlyComplicated); gen.setNextIsAttribute(false);        }    }    private void writeMap(ToXmlGenerator toXmlGenerator) throws IOException {        for (Map.Entry<String, Object> entry : map.entrySet()) { toXmlGenerator.writeObjectField(entry.getKey(), entry.getValue());        }    }    @Override    public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {        serialize(gen, serializers);    }    private String overlyComplicated;    private Map<String, Object> map;    // getters, setters, toString}

上面的代码打印:

<ObjectNode>  <Examples overlyComplicated="yes">    <Puppy>true</Puppy>    <Apple>2</Apple>    <Jet>Li</Jet>  </Examples>  <Single>    <One>1</One>  </Single></ObjectNode>

如果要使用相同的对象

Example

POJO
进行
JSON
序列化,则需要在
serialize
方法中处理它或创建另一个对象
ObjectNode
而不是
Examlples
对象。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5091906.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存