mba12
mba12

Reputation: 2792

Jackson XML mapper forcing lowercase XML tags

I am trying to convert a POJO with XMLElement annotations into an XML string. I have made the variable names capitalized and the annotation names are also capitalized.

When I run the code snippet below I wind up with the XML example below, all the tag names are lowercase. I have been experimenting with enabling/disabling features but have not found anything that would turn on/off forcing lower case XML tag out.

Value of String xml variable below:
ACTUAL OUTPUT:
<root><field1>value</field1><field2>value2<field2><field3>value3<field3><root>

EXPECTED OUTPUT:
<root><Field1>value</Field1><Field2>value2<Field2><Field3>value3<Field3><root>


public class Object1 {
    @XmlElement(name = "Field1", required = true)
    protected String Field1;
    @XmlElement(name = "Field2", required = true)
    protected String Field2;
    @XmlElement(name = "Field3", required = true)
    protected String Field3;
}

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.databind.*;

...


JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper xmlMapper = new XmlMapper(xmlModule);
xmlMapper.disable( MapperFeature.USE_STD_BEAN_NAMING );

// convert the object into xml string
// object1 is an instance of Object1 above with assigned values 
String xml = xmlMapper.writeValueAsString(object1);

Upvotes: 1

Views: 5345

Answers (1)

DwB
DwB

Reputation: 38290

This is a solution, but perhaps not the answer you are seeking.

Replace the @XmlElement annotations with @JacksonXmlProperty annotations. Use the localName attribute to set the tag name.

Here is an example: @JacksonXmlProperty(localName = "Field1")

Upvotes: 3

Related Questions