title: Jackson xml ignore empty Objects when serializing
body: <p>I want to create a xml from a html form with the help of a Spring MVC ModelAttribute (Person) and Jackson.</p>
  
  <p>form:</p>
  
  <pre><code>....
  &lt;input name="name"/&gt;
  &lt;input name="birthday"/&gt;
  &lt;input name="address.street/&gt;
  &lt;input name="address.city/&gt;
  ....
  </code></pre>
  
  <p>POJO:</p>
  
  <pre><code>@JacksonXmlRootElement(localName = "person")
  @JsonInclude(Include.NON_DEFAULT)
  public class Person implements Serializable {
      @JsonIgnore
      private Long id;
      @JacksonXmlProperty(localName = "name")
      private String name;
      @JacksonXmlProperty(localName = "email")
      private String email;
      @JacksonXmlProperty(localName = "address")
      private Address address;
      private String birthday;
  
  //getter and setter
  
  }
  
  @JsonInclude(Include.NON_EMPTY)
  public class Address implements Serializable {
      private Long id;
      private String street;
      private String streetNumber;
      private String postalcode;
      private String city;
  
     //getter and setter
  }
  </code></pre>
  
  <p>create XML in Controller:</p>
  
  <pre><code>@RequestMapping("saveperson")
  public String savePerson(@ModelAttribute final Person person, final ModelMap model) {
      final ObjectMapper xmlMapper = new XmlMapper();
      final String xml = xmlMapper.writeValueAsString(person);
      return "redirect:/listpersons";
  }
  </code></pre>
  
  <p>output xml:</p>
  
  <pre><code>    &lt;person&gt;
          &lt;name&gt;John&lt;/name&gt;
          &lt;email&gt;john@test.com&lt;/email&gt;
          &lt;address/&gt;
      &lt;/person&gt;
  </code></pre>
  
  <p>How can i exclude the empty Objects from XML?</p>
  
  <p>I tried to set all empty strings in the ModelAttribute to null:</p>
  
  <pre><code>@InitBinder
  public void initBinder(WebDataBinder binder) {
      binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  }
  </code></pre>
  
  <p>But it doesn't work.
  I also tried
  @JsonInclude(Include.NON_EMPTY) and @JsonInclude(Include.NON_NULL)
  But it always includes empty objects in the XML.</p>
  
  <p>My second question is: When i want to bind a list of addresses to the html form. What is the best way to do this?</p>
  
  <pre><code>....
  &lt;input name="name"/&gt;
  &lt;input name="birthday"/&gt;
  &lt;input name="address1.street/&gt;
  &lt;input name="address1.city/&gt;
  &lt;input name="address2.street/&gt;
  &lt;input name="address2.city/&gt;
  ....
  
  @JacksonXmlRootElement(localName = "person")
  @JsonInclude(Include.NON_DEFAULT)
  public class Person implements Serializable {
      @JsonIgnore
      private Long id;
      @JacksonXmlProperty(localName = "name")
      private String name;
      @JacksonXmlProperty(localName = "email")
      private String email;
      @JsonIgnore
      private Address address1;
      @JsonIgnore
      private Address address2;
  //add address1 and address2 in the getter or dto for the ModelAttribute?
      @JacksonXmlElementWrapper(localName = "addresses")
      @JacksonXmlProperty(localName = "address")
      private List&lt;Address&gt; address;
      private String birthday;
  
  //getter and setter
  
  }
  </code></pre>
  
  <p>Thanks!</p>