jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

unnecessary xmlns="" in the root element

Open rgururaj opened this issue 14 years ago • 22 comments

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

and with POJO like:

public class Simple { public int x = 1; public int y = 2; }

you would get something like:

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

There is a xmlns="" attribute at the root element, which was not expected. Using version 2.0.4 (dataformat-xml) with 2.0.5 (core)

rgururaj avatar Aug 26 '12 20:08 rgururaj

Which Stax XML processor are you using, and which JDK? I have not been able to reproduce this issue via unit tests myself previously. That extra namespace is harmless, for what that's worth, i.e. it's not exactly a bug. But ideally we would not add it, of course, since it's just fluff.

tatu-at-salesforce avatar Aug 27 '12 17:08 tatu-at-salesforce

JDK version 1.6.0_33 (Mac OS X 10.7) So I am using the StAX processor that came with the JDK I guess.

rgururaj avatar Aug 27 '12 17:08 rgururaj

Ok. That would explain it, as github project explicitly uses Woodstox. I'll have to see if there's a way to prevent SJSXP (sun default) from producing those, perhaps by pre-registering namespace binding or something.

tatu-at-salesforce avatar Aug 27 '12 17:08 tatu-at-salesforce

I have the same problem too, and is especially frustrating because I want to define a specific namespace (and have done so by setting the namespace attribute of the @JacksonXmlRootElement, @JacksonXmlProperty and @JacksonXmlProperty accordingly.

The result is that my root element contains twice the xmlns attribute.

I am running on: JDK version 1.6.0_35 (Mac OS X 10.7.4)

Here is my test below:

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

public class RootXmlnsTest {

    private static final String NS = "http://www.mytest.com";

    @JacksonXmlRootElement(localName="testRoot", namespace=NS)
    static class TestCls{

        private String thisOne;

        private String thatOne;

        public TestCls(){}

        public TestCls( String one, String two){
            this.thisOne = one;
            this.thatOne = two;
        }

        @JacksonXmlProperty(isAttribute = true, localName = "thisone", namespace=NS)
        public String getThisOne() { return thisOne; }

        public void setThisOne(String thisOne) { this.thisOne = thisOne; }

        @JsonProperty("thatone")
        @JacksonXmlProperty(namespace = NS)
        public String getThatOne() {return thatOne;}

        public void setThatOne(String thatOne) {this.thatOne = thatOne;}
    }

    @Test(expected=java.lang.AssertionError.class)
    public void testXmlns() throws JsonGenerationException, JsonMappingException, IOException{
        XmlMapper mapper = new XmlMapper();

        TestCls t = new TestCls( "one", "two");

        String got = mapper.writeValueAsString(t);

        assertThat("xml", got, is("<testRoot xmlns=\"http://www.mytest.com\" thisone=\"one\"><thatone>two</thatone></testRoot>") );

    }
}

farfalena-zz avatar Sep 19 '12 15:09 farfalena-zz

Quick question @farfalena -- are you also using the default Stax (Sjsxp) from JDK? If you add Woodstox (4.1.2) dependency, does it work better?

tatu-at-salesforce avatar Sep 19 '12 17:09 tatu-at-salesforce

@farfalena Your test is slightly broken: attributes can NOT use the default namespace, so XML string being given does not match definition. attribute 'thisone' does not have namespace, unless an explicit namespace binding is used.

This is why there has to be at least one explicitly bound namespace; and element either must use that one, or use default namespace. And XML writer tends to default to using default namespace for elements, although at the end of the day, there is no real way to force a specific behavior there (Stax API does have a way to suggest prefix to use, but there are no annotations to support that).

cowtowncoder avatar Sep 20 '12 05:09 cowtowncoder

@rgururaj: I can see the extra namespace being produced, but since serializer uses so-called repairing mode, and specifically does NOT use any methods to (try to) force writing of namespace declarations, this is a bug in SJSXP. It should not add extra bindings; and in fact may even produce invalid XML for case where root element uses a non-empty namespace.

So the solution is to use Woodstox or Aalto as the Stax provider -- I can't think of a way to work around this SJSXP deficiency. Tests already use Woodstox, for what it is worth, so you can see Maven dependency from pom.xml; just leave out scope definition 'test'.

cowtowncoder avatar Sep 20 '12 05:09 cowtowncoder

Indeed using the "Woodstox stax provider" results in getting rid of the unnecessary xmlns.

However now I see how my test was wrong concerning the use of namespace in attributes. With the default stax provider of the jdk however, using those annotation was the only way to have the XML (along with child elements and attributes) exported as expected in the test - except of course of the additional xmlns attribute in the root element.

So as far as I understand it is kind of mandatory to use the Woodstox parser together with your library.

Thank you once again for your support.

farfalena-zz avatar Sep 20 '12 11:09 farfalena-zz

Yes, looks like default Stax implementation's repairing mode is broken. This is very unfortunate -- I sent bug reports and patches for about dozen issues with SJSXP couple of years ago, and by now they are included. But as far as I know, no one actively works on it. And yet SJSXP officially passes the TCK for Stax...

So I think it is fair to say that one has to use either Woodstox or Aalto, both of these are known to work. Plus I have committer rights for both projects so at least I could fix if they had issues.

I hope things work with Woodstox/Aalto at least; and upcoming 2.1 should help in many areas of XML module.

cowtowncoder avatar Sep 20 '12 15:09 cowtowncoder

Thank you very much for your help. I used Woodstox and it work nicely now. Keep up the great work!

farfalena-zz avatar Sep 20 '12 16:09 farfalena-zz

How does one configure the XmlMapper to use Woodstox?

jtdeane avatar Dec 14 '12 17:12 jtdeane

You need to construct XmlFactory with Woodstox-provided XMLInputFactory and XMLOutputFactory. To do that just construct Wodostox implementations (WstxInputFactory and WstxOutputFactory I think).

cowtowncoder avatar Dec 14 '12 17:12 cowtowncoder

It works fine with aalto:

import com.fasterxml.aalto.stax.InputFactoryImpl; import com.fasterxml.aalto.stax.OutputFactoryImpl;

XmlFactory factory = new XmlFactory( new InputFactoryImpl(), new OutputFactoryImpl() );

XmlMapper mapper = new XmlMapper(factory);

MrForms avatar Sep 05 '13 03:09 MrForms

Can someone confirm what the recommended way to solve this is?

lisadesouza89 avatar Jul 16 '15 09:07 lisadesouza89

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.

cowtowncoder avatar Jul 16 '15 22:07 cowtowncoder

In general, and according to the XML standard including xmlns="" should NOT change the meaning of the XML tags. That said, SAX builders (for example with JDOM2) appear to get upset if the empty namespace is declared.

To assist others who find this post like I did, adding the following dependency will import woodstox

<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>woodstox-core-asl</artifactId>
    <version>4.1.2</version>
</dependency>

And the following lines will update your XmlMapper to use WoodStox

XmlFactory factory = new XmlFactory(new WstxInputFactory(), new WstxOutputFactory());
XmlMapper xmlMapper = new XmlMapper(factory);

dewthefifth avatar Jul 28 '16 22:07 dewthefifth

Thank you for additional notes on how to resolve this, if it is an issue (which as you correctly pointed out should not occur, but some tools have their own quirks).

cowtowncoder avatar Jul 29 '16 14:07 cowtowncoder

i am seeing same issue with Spring boot 1.4.1 and woostox 1.4.x, it is generating blank name space. how to fix this ?

pradeepkusingh avatar Mar 22 '17 16:03 pradeepkusingh

@pradeepkusingh I have not seen Woodstox do this ever, so a reproduction would be needed: stand-alone one, not via Spring Boot. More commonly this is due to some other Stax implementation being used so you may want to double check to try to see what implementation is being used. If you can reproduce this with Woodstox 4.0 with such a test please file a new issue as this one is closed.

cowtowncoder avatar Mar 22 '17 16:03 cowtowncoder

I have a usecase where I need the xmlns namespace attribute as a key value pair in the generated json from xml . But currently I am not able to do so as the namespace is getting ignored.

xml is <?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"> <edmx:Reference Uri="../VOC_Core/$metadata"> <edmx:Include Namespace="Org.OData.Core.V1" Alias="Core" /> </edmx:Reference> <edmx:DataServices> <Schema Namespace="EPMSample" xmlns="http://docs.oasis-open.org/odata/ns/edm"> </Schema> </edmx:DataServices> </edmx:Edmx>

generated json { "Version":"4.0", "Reference":{ "Uri":"../VOC_Core/$metadata", "Include":{ "Namespace":"Org.OData.Core.V1", "Alias":"Core" } }, "DataServices":{ "Schema":{ "Namespace":"EPMSample" } } }

My code is as follows

XmlMapper xmlMapper = new XmlMapper(); JsonNode node = xmlMapper.readTree(xml.getBytes()); ObjectMapper jsonMapper = new ObjectMapper(); String json = jsonMapper.writeValueAsString(node); How can I include the xmlns attribute as a key value pair in the json What all configuration do I need to do so , as to retains the xmlns attribute. ?

JayantNayak avatar Nov 29 '17 09:11 JayantNayak