7

I'm trying to parse my object to XML using jackson-dataformat-xml and, when i set the root namespace and parse the file, all properties of my object in the XML gives a empty namespace xmlns="". On jackson's github docs, is advise to use woodstox instead stax XML implementation to solve this but, the behavior still exists.

This is my pojo:

@JacksonXmlRootElement(namespace = "https://www.google.com.br")
public class Cliente implements Serializable {
 // Omitted
}

And my parse code:

Cliente cliente = new Cliente();
cliente.setId(new Long(1));
cliente.setNome("Tiago Cassio".toUpperCase());
cliente.setSobrenome("da Conceicao".toUpperCase());
Endereco endereco = new Endereco();
endereco.setId(new Long(1));
endereco.setLogradouro("blablabla");
endereco.setNumero("999");
endereco.setCep("99999");
endereco.setBairro("blablabla");
cliente.setEndereco(endereco);
ObjectMapper mapper = new XmlMapper();
System.out.println(mapper.writeValueAsString(cliente));

This is the XML generated:

<Cliente xmlns="https://www.google.com.br">
    <id xmlns="">1</id>
    <nome xmlns="">TIAGO CASSIO</nome>
    <sobrenome xmlns="">DA CONCEICAO</sobrenome>
    <endereco xmlns="">
        <id>1</id>
        <logradouro>blablabla</logradouro>
        <numero>999</numero>
        <cep>99999</cep>
        <bairro>blablabla</bairro>
    </endereco>
</Cliente>

Any idea where is the problem? My project is under a Spring boot version 1.3.0.M5. Thanks for all.

Tiago Cássio
  • 301
  • 2
  • 5
  • 1
    Do you have the properties of the `Cliente` annotated with `@JacksonXmlProperty` with the `namespace` attribute set on them? If not, can you try doing that to see if that helps? – mystarrocks Oct 13 '15 at 13:16
  • I've already done, still not working. The curious is when leave the object root without namespace, all properties namespaces is gone. Thanks for your help! – Tiago Cássio Oct 13 '15 at 13:28
  • 1
    If your root element is in a namespace and the child elements aren't, they need to carry the namespace undeclaration to cancel the namespace declaration on the root. If everything is in no namespace, the namespace undeclaration isn't needed. – Michael Kay Oct 13 '15 at 14:53
  • 1
    I undestand but, i need the namespace declaration only in root element. how i can do this? – Tiago Cássio Oct 13 '15 at 16:00
  • I was able to reproduce this and get around using `@JacksonXmlProperty (namespace="https://www.google.com.br")` on the properties. Can you check your change again? – mystarrocks Oct 13 '15 at 17:42
  • 1
    @TiagoCássio you do need to specify namespace for child elements too, otherwise they require namespace of "", which is why such declaration is added. `@JacksonXmlProperty` is one way to do that, and probably simplest. Unfortunately there is no way to specify something like "use this namespace for all properties of this class" (you could file an RFE for jackson xml module if you want, suggesting something easier) – StaxMan Oct 13 '15 at 18:42
  • Is there any update on this question? – Harshit patel Nov 30 '15 at 07:19
  • @StaxMan Thanks, that explains it for me. I think your comment would qualify as an answer for this question. – Dario Seidl Jan 29 '19 at 12:20
  • There exists already an issue for this: https://github.com/FasterXML/jackson-dataformat-xml/issues/18 – Dario Seidl Jan 29 '19 at 12:24

0 Answers0