FasterXML / jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Apache License 2.0
574 stars 222 forks source link

How to avoid wstxns1: prefix #666

Open mailsanchu opened 3 months ago

mailsanchu commented 3 months ago

Given the following classes RootNamespace.java

@JacksonXmlRootElement(namespace = "namespace1", localName = "RootNamespace")
public class RootNamespace {
    @JacksonXmlProperty(namespace = "namespace1", localName = "childNamespace")
    private ChildNamespace childNamespace;
}

ChildNameSpace.java

public class ChildNamespace {
    @JacksonXmlProperty(namespace = "namespace2", localName = "Name")
    private ThirdLevel thirdLevel;
}

ThirdLevel.java

@Setter
@Getter
public class ThirdLevel {
    @JacksonXmlProperty(namespace = "namespace2", localName = "Name")
    private String name;
}

and It is generating the following output when I convert it into string

<?xml version="1.0" encoding="UTF-8"?>
<RootNamespace xmlns="namespace1">
  <childNamespace>
    <wstxns1:Name xmlns:wstxns1="namespace2"/>
  </childNamespace>
</RootNamespace>

and I would like to generate it the following

<RootNamespace xmlns="namespace1">
    <childNamespace>
        <Name xmlns="namespace2"/>
    </childNamespace>
</RootNamespace>

Is it possible to do. Any help is greatly appreciated.

cowtowncoder commented 3 months ago

To do this, you will probably need to construct XMLStreamWriter, initialize it with namespace binding, and pass that to XMLMapper.writeValue(). I don't think there is a way to avoid automated namespace declaration generation. There is a way in Woodstox to re-configure prefix to use, I think.