28

Hi I need to create an XML from JAVA using Jackson-dataformat XMLMapper. The XML should be like

<Customer>
  <id>1</id>
  <name>Mighty Pulpo</name>
    <addresses>
      <city>austin</city>
      <state>TX</state>
    </addresses>
    <addresses>
      <city>Hong Kong</city>
      <state>Hong Kong</state>
    </addresses>
</Customer>

But I get it always like with an extra "< addresses> < /addresses>" tag.

<Customer>
  <id>1</id>
  <name>Mighty Pulpo</name>
<addresses>
    <addresses>
      <city>austin</city>
      <state>TX</state>
    </addresses>
    <addresses>
      <city>Hong Kong</city>
      <state>Hong Kong</state>
    </addresses>
<addresses>
</Customer>

I am using below code to create XML

JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(jaxbAnnotationModule);
mapper.registerModule(new GuavaModule());
String xml = mapper.writeValueAsString(customer);
System.out.println(xml);

Please can some one help me? How can I remove the extra tag please. I have tried to use @XmlElement but it does not help help. TIA!!

4
  • You are having list of address(Multiple) in customer class. so you need to have parent node addresses and its child node will be adress.
    – ManojP
    Nov 26, 2014 at 8:13
  • Thx Manoj, but I do not want any parental node. Can we somehow omit the parental node? Nov 26, 2014 at 8:15
  • 1
    I got the answer - Just need to use @JacksonXmlElementWrapper(useWrapping = false) Thx for spending time to read it. Nov 26, 2014 at 8:45
  • real question is how to get back. good luck with that.
    – mjs
    Jul 20, 2021 at 9:14

3 Answers 3

62

Try the below code

@JacksonXmlRootElement(localName = "customer") 
class Customer {

    @JacksonXmlProperty(localName = "id")
    private int id;
    @JacksonXmlProperty(localName = "name")
    private String  name;

    @JacksonXmlProperty(localName = "addresses")
    @JacksonXmlElementWrapper(useWrapping = false)
    private Address[] address;

    // you can add it on getter method instead of declaration.  
    @JacksonXmlElementWrapper(useWrapping = false)
    public Address[] getAddress(){ 
        return address;
   }

   //getters, setters, toString             
}

class Address {

    @JacksonXmlProperty(localName = "city")
    private String city;

    @JacksonXmlProperty(localName = "state")
    private String state;
    // getter/setter 
}
0
6

This setting changes default wrapping behavior, if you don't want to deal with annotation everywhere in your code.

XmlMapper mapper = new XmlMapper();
mapper.setDefaultUseWrapper(false);
3

Just to add to ManojP's answer, if adding the @JacksonXmlElementWrapper(useWrapping = false) on the declaration of your variable doesn't work (which was the case for me), adding it to the getter method will do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.