Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove namespace definition from jackson Xml parsing

Tags:

java

xml

jackson

I'm using Jackson data format for serializing Pojos as XML.

It is working fine but I would like to remove the namespace definition:

@JacksonXmlRootElement(localName="simple_something")
public class Simple {
    public int x = 1;
    public int y = 2;
}

I do:

ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(new Simple());

I get:

<simple_something xmlns="">
  <x>1</x>
  <y>2</y>
</simple_something>

but I would like to remove the xmlns=""

and that it looks like

<simple_something>
  <x>1</x>
  <y>2</y>
</simple_something>

Any ideas?

like image 747
Cecilia Arenas Avatar asked Nov 01 '22 08:11

Cecilia Arenas


1 Answers

Make sure to use Woodstox Stax implementation, and not Stax implementation Oracle bundles with JDK. This is usually done by adding Maven dependency to explicitly include woodstox jar. This is explained on XML module README at https://github.com/FasterXML/jackson-dataformat-xml/

Oracle's implemention adds that declaration in namespace-repairing mode for some reason. It is also slower and has more bugs, so there's not much reason to rely on it, unless you really want to minimize external dependencies.

Also note that that namespace declaration is completely benign, so while unnecessary it is legal XML. So while it is eyesore all xml tools should work just fine with such extra declarations.

like image 120
StaxMan Avatar answered Nov 09 '22 12:11

StaxMan