title: Jackson/JAXB serialize List without the wrapper element
body: <p>I am trying to use <a href="https://github.com/FasterXML/jackson-dataformat-xml" rel="nofollow">Jackson's XML annotations</a> 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.</p>
  
  <p>My class looks like:</p>
  
  <pre><code>@JacksonXmlRootElement(localName = "request")
  public class TrackingEvent {
      private List&lt;JAXBElement&lt;String&gt;&gt; data = new ArrayList&lt;JAXBElement&lt;String&gt;&gt;();
  
      public TrackingEvent() {}
  
      public TrackingEvent addData(String key, String value) {
          data.add(new JAXBElement&lt;String&gt;(new QName(key), String.class, value));
          return this;
      }
  
      public Map&lt;String, String&gt; getData() {
          Map&lt;String, String&gt; dataAsMap = new HashMap&lt;String, String&gt;(data.size());
          for (JAXBElement&lt;String&gt; elem : data) {
              dataAsMap.put(elem.getName().getLocalPart(), elem.getValue());
          }
          return Collections.unmodifiableMap(dataAsMap);
      }
  }
  </code></pre>
  
  <p>My test code (quick-n-dirty, not an actual test):</p>
  
  <pre><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></pre>
  
  <p>And here is the XML it generates:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;request&gt;
    &lt;data&gt;
      &lt;eVar17&gt;woohoo&lt;/eVar17&gt;
    &lt;/data&gt;
  &lt;/request&gt;
  </code></pre>
  
  <p>But what I am looking for is:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;request&gt;
    &lt;eVar17&gt;woohoo&lt;/eVar17&gt;
  &lt;/request&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>I am pretty sure I can use JAXB annotations as well from reading <a href="https://github.com/FasterXML/jackson-module-jaxb-annotations" rel="nofollow">Jackson's docs</a>, but haven't found anything via Google there either.</p>
  
  <p>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.</p>
  
  <h2>My Solution</h2>
  
  <p>I decided to abandon any use of Jackson as the XML I need is not that complex, but forcing Jackson to do what I wanted to was getting increasingly complex.  I decided to use STAX2 to write the XML directly using the same implementation that Jackson uses under the covers.  This allows me to customize the XML however I want.</p>
  
  <p>Blaise's answer sure seemed like it would have done exactly what I wanted though.</p>