Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize Java List to XML using Jackson XML mapper

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!!

like image 443
Sharmistha Sinha Avatar asked Nov 26 '14 08:11

Sharmistha Sinha


People also ask

Can ObjectMapper be used for XML?

Configuration of XML Mapper Object XmlMapper extends ObjectMapper . Therefore, you can use XML in the same way that you use ObjectMapper . For example, register the Java 8 modules to enable the feature of parameter names, Java 8 time, and Java 8 data types.

Does Jackson parse XML?

Jackson is a library for handling JSON in Java systems and now has support for XML from version 2. DOM4J is a memory-efficient library for parsing XML, XPath, and XSLT (eXtensible Stylesheet Language).

What is XML serialization in Java?

Serialization of Java Objects to XML can be done using XMLEncoder, XMLDecoder. Java Object Serialization feature was introduced in JDK 1.1. Serialization transforms a Java object or graph of Java object into an array of bytes which can be stored in a file or transmitted over a network.


2 Answers

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 
}
like image 145
ManojP Avatar answered Oct 20 '22 08:10

ManojP


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);
like image 6
A Kunin Avatar answered Oct 20 '22 10:10

A Kunin