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

标签 java json xml jackson xml-namespaces

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/

相关文章:

python - 如何启用 python 的 json 包来编码 attrdict.AttrDict 对象?

java - 更改 Jackson 中的默认序列化行为

python - 如何使用 Python ElementTree 提取 xml 属性

java - 在 Pentaho Data Integration KETTLE 中迭代重复的特定 HL7 段

java - "Closing"阻塞队列

java - 如何删除Camel中的空目录?

java - JAX-RS 将 JSON 子属性解码到 HashMap

xml - XSD 允许双倍减半

java - XML 解析器 Jar 混淆

java - 如何为 jar 配置 log4j 日志记录?