2

The Manual of Jackson XML write Object like this

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

in https://github.com/FasterXML/jackson-dataformat-xml but in my code,it has [xmlns="" ],and it can't be in my business.

My code:

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>
    }
}
MonKong
  • 31
  • 1
  • 5

2 Answers2

1

As described in the last posting to 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.

1

Just adding woodstox dependency already resolve your question.

<dependency>
  <groupId>com.fasterxml</groupId>
  <artifactId>jackson-xml-databind</artifactId>
  <version>0.6.2</version>
</dependency>
<dependency>
  <groupId>org.codehaus.woodstox</groupId>
  <artifactId>woodstox-core-asl</artifactId>
  <version>4.4.1</version>
</dependency>

And you can use default (de)serialization

XmlMapper mapper = new XmlMapper();
bpedroso
  • 4,617
  • 3
  • 29
  • 34