Skip to content

XML INDENT_OUTPUT property fails to add newline/indent initial elements #172

@rpatrick00

Description

@rpatrick00

I am using Jackson 2.6.3 with JAXB annotations using the following code:

    XmlMapper mapper = new XmlMapper();

    mapper.registerModule(new JaxbAnnotationModule());
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    mapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
    mapper.writeValue(fileOutputStream, myCompany);

The file I get looks like the following. How do I get Jackson to add the newlines and missing indentation?

<?xml version='1.1' encoding='UTF-8'?><company><employees>
    <employee id="emp-1">
      <name>Robert Patrick</name>
      <type>FULLTIME</type>
    </employee>
    <employee id="emp-2">
      <name>Michael Smith</name>
      <type>CONTRACTOR</type>
    </employee>
  </employees>
</company>

The Java classes look like this.

Company.java:

@XmlRootElement(name = "company")
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {

  @XmlElementWrapper(name = "employees")
  @XmlElement(name = "employee")
  private List employees;

  public Company() {
    employees = new ArrayList();
  }
...
}

Employee.java:

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
  @XmlAttribute
  @XmlID
  private String id;

  @XmlElement
  private String name;

  @XmlElement
  private EmployeeType type;
  ...
}

EmployeeType:

@XmlEnum
public enum EmployeeType {
  @XmlEnumValue("FULLTIME")
  FULLTIME("FULLTIME"),
  @XmlEnumValue("PARTTIME")
  HOURLY("PARTTIME"),
  @XmlEnumValue("CONTRACTOR")
  CONTRACTOR("CONTRACTOR");

  @XmlValue
  private String value;

  private static final String DEFAULT_VALUE = "FULLTIME";

  EmployeeType() {
    this.value = DEFAULT_VALUE;
  }

  EmployeeType(String value) {
    this.value = value;
  }

  public String value() {
    return value;
  }

  @Override
  public String toString() {
    return value;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions