Si vous essayez de produire du XML indenté à l'aide de l'implémentation JAXB du JDK (implémentation intégrée au JDK jusqu'à JDK 10 = `com.sun.xml.bind: jaxb-impl '), l'indentation est un espace demi-largeur par défaut. Ce sera 4 caractères.
modèle
@XmlRootElement
public class User {
  private String id;
  private String name;
  public void setId(String id) {
    this.id = id;
  }
  public String getId() {
    return id;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return name;
  }
}
Traitement de la sortie XML à l'aide de JAXB
User user = new User();
user.setId("001");
user.setName("Kazuki");
Marshaller marshaller = JAXBContext.newInstance(User.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(user, writer);
System.out.println(writer.toString());
XML de sortie
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <id>001</id>
    <name>Kazuki</name>
</user>
Lorsque vous utilisez l'implémentation JAXB du JDK, vous pouvez spécifier une chaîne d'indentation dans la propriété com.sun.xml.internal.bind.indentString.
Exemple de spécification de chaîne de caractères de retrait
User user = new User();
user.setId("001");
user.setName("Kazuki");
Marshaller marshaller = JAXBContext.newInstance(User.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.internal.bind.indentString", "  "); //2 espaces à un octet
StringWriter writer = new StringWriter();
marshaller.marshal(user, writer);
System.out.println(writer.toString());
XML de sortie
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
  <id>001</id>
  <name>Kazuki</name>
</user>
Avec l'implémentation JAXB du JDK, il était possible de changer facilement le nombre de caractères indentés. Qu'en est-il des autres implémentations! ??