Hi I need to create an XML from JAVA using Jackson-dataformat XMLMapper.
The XML should be like
1
Mighty Pulpo
austin
TX
Hong Kong
Hong Kong
But I get it always like with an extra "< addresses> < /addresses>" tag.
1
Mighty Pulpo
austin
TX
Hong Kong
Hong Kong
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!!
解决方案
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;
//getters, setters, toString
}
class Address {
@JacksonXmlProperty(localName = "city")
private String city;
@JacksonXmlProperty(localName = "state")
private String state;
// getter/setter
}