Home > Enterprise >  How to dynamically convert JSON to XML with custom namespace?
How to dynamically convert JSON to XML with custom namespace?

Time:08-25

I try to convert a JSON into an XML with the following code

final ObjectMapper objectMapper = new ObjectMapper();
final XmlMapper xmlMapper = new XmlMapper();

JsonNode jsonNode = objectMapper.readTree(jsonString);

String xmlString = xmlMapper
   .writerWithDefaultPrettyPrinter()
   .withRootName("rootname")
   .writeValueAsString(jsonNode);

Basically it works. Does anyone know, how I can add a namespace to the serialized XML-attributes. I've no POJOs for the objects. The convert should generate from this

{
    "Status" : "OK"
}

something like this:

<ns2:rootname xmlns:ns2="http://whatever-it-is.de/">
  <ns2:state>OK</ns2:state>
</ns2:rootname>

CodePudding user response:

just create a pojo and add jackson annotations, e.g.

@JacksonXmlProperty(localName="ns2:http://whatever-it-is.de/")
public class Status {
// ...
}

Or if you want to go without a pojo try a custom serializer which adds namespaces

https://www.baeldung.com/jackson-custom-serialization

CodePudding user response:

You need to provide custom Json Node serialiser and use ToXmlGenerator. See below example:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

import javax.xml.namespace.QName;
import java.io.IOException;

public class XmlMapperApp {

    public static void main(String... args) throws Exception {
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);

        ObjectNode node = xmlMapper.createObjectNode().put("Status", "OK");

        xmlMapper.registerModule(new SimpleModule().addSerializer(JsonNode.class, new JsonSerializer<JsonNode>() {
            @Override
            public void serialize(JsonNode jsonNode, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                ToXmlGenerator xmlGenerator = (ToXmlGenerator) jsonGenerator;

                xmlGenerator.setNextName(new QName("http://whatever-it-is.de/", "rootname", "anything"));
                xmlGenerator.writeStartObject();
                jsonNode.fields().forEachRemaining(e -> {
                    try {
                        xmlGenerator.writeFieldName(e.getKey());
                        xmlGenerator.writeString(e.getValue().asText());
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                });
                jsonGenerator.writeEndObject();
            }
        }));

        System.out.println(xmlMapper.writeValueAsString(node));
    }
}

Above example prints:

<wstxns1:rootname xmlns:wstxns1="http://whatever-it-is.de/">
  <wstxns1:Status>OK</wstxns1:Status>
</wstxns1:rootname>
  • Related