Jackson/JAXB serialize List without the wrapper element

lotfio

New Member
I am trying to use Jackson's XML annotations to serialize a Map to XML. I am converting the Map to a List in order to use the map keys as the element names and the values as the element values, and I am so close to getting the desired XML.My class looks like:\[code\]@JacksonXmlRootElement(localName = "request")public class TrackingEvent { private List<JAXBElement<String>> data = http://stackoverflow.com/questions/14406128/new ArrayList<JAXBElement<String>>(); public TrackingEvent() {} public TrackingEvent addData(String key, String value) { data.add(new JAXBElement<String>(new QName(key), String.class, value)); return this; } public Map<String, String> getData() { Map<String, String> dataAsMap = new HashMap<String, String>(data.size()); for (JAXBElement<String> elem : data) { dataAsMap.put(elem.getName().getLocalPart(), elem.getValue()); } return Collections.unmodifiableMap(dataAsMap); }}\[/code\]My test code (quick-n-dirty, not an actual test):\[code\]@RunWith(JUnit4.class)public class TrackingEventMapperTest { @Test public void eventToXml() throws Exception { TrackingEvent e = new TrackingEvent().addData("eVar17", "woohoo"); XmlMapper mapper = new XmlMapper(); mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); System.out.println("\n" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(e)); }}\[/code\]And here is the XML it generates:\[code\]<?xml version='1.0' encoding='UTF-8'?><request> <data> <eVar17>woohoo</eVar17> </data></request>\[/code\]But what I am looking for is:\[code\]<?xml version='1.0' encoding='UTF-8'?><request> <eVar17>woohoo</eVar17></request>\[/code\]I have tried adding a @JacksonXmlElementWrapper(useWrapping = false) annotation to both the data declaration and its getter, but that appears to do nothing. I can change the name of the element adding a @JacksonXmlProperty(localName = "moop") to the declaration/getter, but cannot figure out how to leave it out entirely.I am pretty sure I can use JAXB annotations as well from reading Jackson's docs, but haven't found anything via Google there either.As you can probably guess, I am trying to create an XML document representing Omniture data, and trying to send all the eVar and prop variables in a way where I don't have to explicitly include them in my POJO.
 
Back
Top