0

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

Aaron Ryu
  • 7
  • 6
  • I'm sorry, but how are you using Kotlin 1.6? 1.3 was just released, 1.6 is far from even having alpha dev done on the release – Zoe Nov 06 '18 at 16:07
  • 1
    @zoe sorry, edited. I used Kotlin 1.3. I confused with JVM version 1.6 Kotlin uses – Aaron Ryu Nov 07 '18 at 03:09
  • Found an interesting solution: https://github.com/FasterXML/jackson-dataformat-xml/issues/18#issuecomment-446546133 – Oles Pankiv Feb 13 '19 at 20:01

1 Answers1

0

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

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Oh my gosh, it was a silly question. I didn't know it will smartly inherit a namespace. Thanks for quick replying on silly question. – Aaron Ryu Nov 06 '18 at 14:15
  • Oh, but as your solution, i should write that namspace to all child classes of "Hello". Is there any solution? for just cut it on "Hello" class level? – Aaron Ryu Nov 06 '18 at 14:19
  • I'm answering out of XML knowledge rather than Java knowledge, so I don't know if there's a convenient way to say "just inherit the namespace" with your particular toolkit. – Jon Hanna Nov 06 '18 at 14:25
  • Ok, Thanks for your answer :) – Aaron Ryu Nov 06 '18 at 14:30