I seem to be having trouble using Jackson to serialize to XML. My code is below:
CONTAINER TEST
package com.test; import java.util.ArrayList; import com.fasterxml.jackson.annotation.JsonProperty; public class TestContainer { private String testContainerID; private String testContainerMessage; private ArrayList<TestChild> testContainerChildren; @JsonProperty("TestContainerID") public String getTestContainerID() { return testContainerID; } @JsonProperty("TestContainerID") public void setTestContainerID(String testContainerID) { this.testContainerID = testContainerID; } @JsonProperty("TestContainerMessage") public String getTestContainerMessage() { return testContainerMessage; } @JsonProperty("TestContainerMessage") public void setTestContainerMessage(String testContainerMessage) { this.testContainerMessage = testContainerMessage; } @JsonProperty("TestContainerChildren") public ArrayList<TestChild> getTestContainerChildren() { return testContainerChildren; } @JsonProperty("TestContainerChildren") public void setTestContainerChildren(ArrayList<TestChild> testContainerChildren) { this.testContainerChildren = testContainerChildren; } }
Testchild
package com.test; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; @JsonRootName(value="TestChild") public class TestChild { private String testChildID; private String testChildMessage; @JsonProperty("TestChildID") public String getTestChildID() { return testChildID; } @JsonProperty("TestChildID") public void setTestChildID(String testChildID) { this.testChildID = testChildID; } @JsonProperty("TestChildMessage") public String getTestChildMessage() { return testChildMessage; } @JsonProperty("TestChildMessage") public void setTestChildMessage(String testChildMessage) { this.testChildMessage = testChildMessage; } }
USE
Serialization:
XmlMapper xm = new XmlMapper (); TestContainer tc = xm.readValue (sb.toString (), TestContainer.class);
Deserialization:
System.out.println (xm.writeValueAsString (dts)); tc = xm.readValue (sb.toString (), TestContainer.class);
What I am doing is loading an XML file from a folder in the classpath and putting the contents of the file in a StringBuffer. The problem is the generated XML for the collection of objects. When writing XML, I want something like:
<TestContainerChildren><TestChild><...(Element Details)...></TestChild></TestContainerChildren>
but I get:
<TestContainerChildren><TestContainerChildren><...(Element Details)...><TestContainerChildren></TestContainerChildren>
I'm not sure what I am missing here. I have no problem with the JSON part of serialization / deserialization, only XML. I tried using Jackson and JAXB annotations to disable packaging, I tried using the following annotations:
- @JsonRootName
- @JsonProperty
- @JacksonXmlElementWrapper
- @JacksonElement
- @XmlElementWrapper
- @XmlElement
I'm pretty sure this is something stupid of me, but any help would be greatly appreciated.
source share