Git Product home page Git Product logo

Comments (22)

tatu-at-salesforce avatar tatu-at-salesforce commented on July 27, 2024

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.

from jackson-dataformat-xml.

rgururaj avatar rgururaj commented on July 27, 2024

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.

from jackson-dataformat-xml.

tatu-at-salesforce avatar tatu-at-salesforce commented on July 27, 2024

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.

from jackson-dataformat-xml.

farfalena-zz avatar farfalena-zz commented on July 27, 2024

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>") );

    }
}

from jackson-dataformat-xml.

tatu-at-salesforce avatar tatu-at-salesforce commented on July 27, 2024

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?

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

@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).

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

@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'.

from jackson-dataformat-xml.

farfalena-zz avatar farfalena-zz commented on July 27, 2024

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.

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

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.

from jackson-dataformat-xml.

farfalena-zz avatar farfalena-zz commented on July 27, 2024

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

from jackson-dataformat-xml.

jtdeane avatar jtdeane commented on July 27, 2024

How does one configure the XmlMapper to use Woodstox?

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

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

from jackson-dataformat-xml.

MrForms avatar MrForms commented on July 27, 2024

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);

from jackson-dataformat-xml.

lisadesouza89 avatar lisadesouza89 commented on July 27, 2024

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

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

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.

from jackson-dataformat-xml.

dewthefifth avatar dewthefifth commented on July 27, 2024

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);

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

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).

from jackson-dataformat-xml.

pradeepkusingh avatar pradeepkusingh commented on July 27, 2024

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 ?

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

@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.

from jackson-dataformat-xml.

JayantNayak avatar JayantNayak commented on July 27, 2024

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. ?

from jackson-dataformat-xml.

cowtowncoder avatar cowtowncoder commented on July 27, 2024

@JayantNayak please do not add comments on closed issues.

For usage questions, please use mailing jackson-users.

from jackson-dataformat-xml.

roridedi avatar roridedi commented on July 27, 2024

Is there an annotation associated with Stax?

from jackson-dataformat-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.