我使用HashMap将其序列化为xml:
var mapper = new XmlMapper();
HashMap<String, Object> hm = new HashMap<>();
hm.put("header", "value");
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(hm));输出
<HashMap>
<header>value</header>
</HashMap>问题是,我需要添加未知的,直到运行时数量的属性到头。
<HashMap>
<header attr1= "text" attr2 = "another text" >value</header>
</HashMap>除了自定义序列化程序之外,还有其他解决办法吗?
发布于 2021-11-11 15:57:14
你可以这样做
public class TestXml {
class HeaderWrapper {
@JacksonXmlProperty(isAttribute = true)
private String attr1 = "text";
@JacksonXmlProperty(isAttribute = true)
private String attr2 = "another text";
private String header = "value"
}
@Test
void test() throws JsonProcessingException {
var mapper = new XmlMapper();
var h1 = new HashMap<>();
h1.put("test", new NullWrapper());
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(h1));
}
}https://stackoverflow.com/questions/69929777
复制相似问题