1

This is my JSON

    {
      "field1": "value1",
      "field2": "value2",
      "field3": "value3",
      "field4": "value4",
      "field5": "value5"
    }

This is XML which I want to convert to:

<root>
    <element1>value1</element1>
    <element2>value2</element2>
    <element3 element4="value4" element5="value5">value3</element3>
</root>

So basically, I want to make element 4 & 5 as attributes to element3. Hope I am making sense so far.

This is what my pojo to parse JSON looks like

public class JSONMessage {

    Date timestamp;

    @JsonProperty("field1")
    @JacksonXmlProperty(localName = "element1")
    String element1;

    @JsonProperty("field2")
    @JacksonXmlProperty(localName = "element2")
    String element2;

    @JsonProperty("field3")
    @JacksonXmlProperty(localName = "element3")
    String element3;

    @JsonProperty("field4")
    @JacksonXmlProperty(localName = "element4")
    String element4;

    @JsonProperty("field5")
    @JacksonXmlProperty(localName = "element5")
    String element5;
}

This is what my pojo to parse JSON to XML looks like

@JacksonXmlRootElement(localName = "linkFoundEvent")
public class XMLMessage {
    private Date element1;
    private String element1;
    private String element2;
    @JacksonXmlProperty(localName = "element3")
    private Element3 element3;

}

And for Element3, I've written this class -

public class Element3{
    @JacksonXmlText
    private String element3;
    @JacksonXmlProperty(localName = "element4", isAttribute = true)
    private String element4;
    @JacksonXmlProperty(localName = "element5", isAttribute = true)
    private String element5;
}

How can I make Element4 and Element5 as attributes to Element4? Please help! Thanks much in advance.

1 Answer 1

3

You don't need two POJO classes (one for JSON & one for XML) to achieve the transformation from input json to output xml (if that's all you want here), check below full working code:

import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

public class JsonXmlTransformation {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        System.out.println(jsonToXml());
    }

    /**
     * json to xml transformation 
     */
    public static String jsonToXml() throws JsonParseException, JsonMappingException, IOException{
        String json = "{\r\n      \"field1\": \"value1\",\r\n      \"field2\": \"value2\",\r\n      \"field3\": \"value3\",\r\n      \"field4\": \"value4\",\r\n      \"field5\": \"value5\"\r\n    }";
        return new XmlMapper().writeValueAsString(new ObjectMapper().readValue(json, Message.class));
    }
}

class Message {

    @JacksonXmlProperty(localName = "element1")
    String element1;

    @JacksonXmlProperty(localName = "element2")
    String element2;

    @JacksonXmlProperty(localName = "element3")
    Elements elements;

    @JsonCreator
    public Message(@JsonProperty("field1") String element1, @JsonProperty("field2") String element2,
            @JsonProperty("field3") String element3, @JsonProperty("field4") String element4, @JsonProperty("field5") String element5) {
        super();
        this.element1 = element1;
        this.element2 = element2;
        this.elements = new Elements(element3, element4, element5); 
    }
}

class Elements{

    public Elements(String element3, String element4, String element5) {
        super();
        this.element3 = element3;
        this.element4 = element4;
        this.element5 = element5;
    }

    @JacksonXmlText
    String element3;

    @JacksonXmlProperty(localName = "element4", isAttribute = true)
    String element4;

    @JacksonXmlProperty(localName = "element5", isAttribute = true)
    String element5;
}

Input:

{
      "field1": "value1",
      "field2": "value2",
      "field3": "value3",
      "field4": "value4",
      "field5": "value5"
    }

Output:

<Message>
    <element1>value1</element1>
    <element2>value2</element2>
    <element3 element4="value4" element5="value5">value3</element3>
</Message>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, but it's not working, the output I am now getting is <element1/><element2/><element3/> and so on.. You said I don't need two class, but you're using two classes, Message & Elements. If I strictly follow the beginning and end of your classes, I can't see that one is a subclass. This line where you're transforming method in the class is little confusing and is giving me JsonParseException. Can you please simplify that? Thanks again for helping here.
I added the standalone executable code, and amended the answer to make it more simplified. Let me know if you still have questions.
Thank you, Amith. I had to change few things around but it's working now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.