Closed
Description
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 (eonezhang@gmail.com)
* @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'
Metadata
Metadata
Assignees
Labels
No labels