xmlkotlinnamespacesxml-namespacespackage-info

Can i ignore child's namespaces from @XmlRootElement


I'm using Kotlin 1.3, Java 8 and Spring 4.0+.

Just i want to attach a namespace only to <HELLO>, not to childs of it. But, when i define the namespace using @XmlRootElement for <HELLO> then <HI> also get a default namespace even i don't define any namespace.

@XmlRootElement(name = "HELLO", namespace = "http://www.hello.net")
public class Hello {

    @XmlElement(name = "HI")
    protected Hi hi;

then

    <HELLO xmlns="http://www.hello.net">
        <HI xmlns=""></HI>
    </HELLO>

Is there any way for removing xmlns="" (a default namespace) from <HI>?

FYI (after finished with an answer):

I'm developing with Kotlin 1.3. I need to use Java Classes generated from XML using xjc(JAXB), because program(API) spec is handled by XML from external client which will communicate with the APIs.

When use xjc without -npa option there is a package-info.java for apply a same namespace on all of classes generated with just two loc and i exported these classes on my project. (ref:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html)

I thought that @XmlSchema(namespace = "namespace1") in package-info.java works well when i register JaxbAnnotationModule() in XmlMapper(with JacksonXmlModule). But, It seems Jackson XmlMapper doesn't fully support xml annotations.

You can solve this problem with below answer or adding XmlMarshaller that add an xmlns to XML.

jackson-dataformat-xml does not support the namespace of package level : https://github.com/FasterXML/jackson-dataformat-xml/issues/18


Solution

  • If you want it to inherit the http://www.hello.net in the XML output then you'll need to give it that namespace.