gpt4 book ai didi

java - 将对象序列化为 XML,无需 [xmlns =""]

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:26 25 4
gpt4 key购买 nike

Jackson XML 手册这样写对象

<Simple>
<x>1</x>
<y>2</y>
</Simple>

https://github.com/FasterXML/jackson-dataformat-xml但在我的代码中,它有 [xmlns=""],而且它不属于我的业务。

我的代码:

public class Sample {
private int id=1;
private String name="abc";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
public static void main(String[] args) throws Exception {
XmlMapper mapper = new XmlMapper();
System.out.println(mapper.writeValueAsString(new Sample()));
//<Sample xmlns=""><id>1</id><name>abc</name></Sample>
}
}

最佳答案

如上一篇文章 issue 32 中所述:

Using Stax implementation other than one that is bundled with JDK: both Woodstox and Aalto work in a way that does not produce this extra (harmless) namespace declaration.

关于java - 将对象序列化为 XML,无需 [xmlns =""],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com