Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to remove unnecessary xmlns="" in the child element #355

Closed
eonezhang opened this issue Aug 13, 2019 · 3 comments
Closed

how to remove unnecessary xmlns="" in the child element #355

eonezhang opened this issue Aug 13, 2019 · 3 comments

Comments

@eonezhang
Copy link

Hello guys,
I want to add namespace to the root element, but the child elements were attached an empty xmlns="", would you guys help me to remove the redundant bugs.

here is my xml output

<Parent xmlns="http://www.163.com/api/v1">
  <name xmlns="">parent</name>
  <Child xmlns="">
    <age>18</age>
  </Child>
</Parent>

I want to remove the xmlns="" in the <name xmlns="">parent</name> and <Child xmlns="">

here is my java code

package utils;

import java.io.IOException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;

import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.stax.WstxOutputFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @author eonezhang ([email protected])
 * @since 2019-08-13-18:30
 */
public class DemoTest {

    public static final String NS = "http://www.163.com/api/v1";

    public static void main(String[] args) throws IOException {
        DemoTest t = new DemoTest();
        t.marshallTest();
    }

    public void marshallTest() throws IOException {
        Parent p = new Parent()
              .setChild(new Child().setAge(18))
              .setName("parent");

        mashall(p);
    }

    private void mashall(Parent p) throws IOException {

        XMLInputFactory ifactory = new WstxInputFactory();
        XMLOutputFactory ofactory = new WstxOutputFactory();
        XmlFactory xf = new XmlFactory(ifactory, ofactory);

        ObjectMapper xmlMapper = new XmlMapper(xf);
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);

        xmlMapper.writeValue(System.out, p);
    }

    @Getter
    @Setter
    @Accessors(chain = true)
    @JacksonXmlRootElement(localName = "Parent", namespace = NS)
    public static class Parent {
        @JacksonXmlProperty(localName = "Child")
        private Child child;
        private String name;
    }

    @Getter
    @Setter
    @Accessors(chain = true)
    public static class Child {
        private Integer age;
    }
}

and here is my dependencies

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.9'
implementation 'com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.9.9'
implementation 'com.fasterxml.woodstox:woodstox-core:5.3.0'
    
@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 13, 2019

That is not a bug but feature. Your Parent has namespace with non-empty URI; Child on the other hand does not, and needs "empty" namespace. That must be declared (with @JacksonXmlProperty which has property for specifying namespace, similar to @JacksonXmlRootElement).

Or, you need to declare same namespace for Child and (I think) name.

@jawpio
Copy link

jawpio commented Apr 14, 2020

The feature is quite poor IMO, as you must annotate actually EACH field with JacksonXmlProperty with the same namespace.

@simon04
Copy link

simon04 commented Feb 26, 2021

Relates to #18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants