How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK

When trying to output indented XML using the JAXB implementation of the JDK (implementation built into the JDK up to JDK 10 = com.sun.xml.bind: jaxb-impl), the indent is a half-width space by default. It will be 4 characters.

model


@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;
  }
}

XML output processing using 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());

Output XML


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <id>001</id>
    <name>Kazuki</name>
</user>

Specify the indent string using the JAXB implementation of the JDK

When using the JAXB implementation of the JDK, you can specify an indent string in the com.sun.xml.internal.bind.indentString property.

Indent character string specification example


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 single-byte spaces
StringWriter writer = new StringWriter();
marshaller.marshal(user, writer);
System.out.println(writer.toString());

Output XML


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
  <id>001</id>
  <name>Kazuki</name>
</user>

Summary

With the JAXB implementation of the JDK, I could easily change the number of indented characters. What about other implementations! ??

Recommended Posts

How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
How to set environment variables in the properties file of Spring boot application
Organized how to interact with the JDK in stages
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to set the log level to be displayed in the release version of orhanobut / logger
Summary of how to use the proxy set in IE when connecting with Java
How to set chrony when the time shifts in CentOS7
How to derive the last day of the month in Java
How to set Lombok in Eclipse
How to set the IP address and host name of CentOS8
Fix the file name of war to the one set in Maven
How to get the id of PRIMAY KEY auto_incremented in MyBatis
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
How to sort the List of SelectItem
How to get the date in java
How to change the maximum and maximum number of POST data in Spark
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
[Swift] How to set an image in the background without using UIImageView.
Android development, how to check null in the value of JSON object
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
Java-database connection Java-MySQL connection ③-2: How to set CLASSPATH to the build bus of Eclipse (Pleiades All in One) / September 2017
[Swift] How to get the number of elements in an array (super basic)
Summary of how to select elements in Selenium
[Order method] Set the order of data in Rails
How to find the cause of the Ruby error
graphql-ruby: How to get the name of query or mutation in controller Note
How to check the logs in the Docker container
Customize how to divide the contents of Recyclerview
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse
How to get the ID of a user authenticated with Firebase in Swift
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
Set the time of LocalDateTime to a specific time
How to add sound in the app (swift)
How to get today's day of the week
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Output of how to use the slice method
[Java] (for MacOS) How to set the classpath
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
How to use JQuery in js.erb of Rails6
How to Install Oracle JDK 1.8 in Ubuntu 18.04 LTS?
How to display the result of form input
How to make a unique combination of data in the rails intermediate table
How to set when "The constructor Empty () is not visible" occurs in junit
How to build the simplest blockchain in Ruby
[Java] How to get the authority of the folder
How to check Rails commands in the terminal
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Java] Implementation method memo to set WS-Security Username Token in SOAP Stub of axis2
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to use git with the power of jgit in an environment without git commands
How to set the retry limit of sidekiq and notify dead queues with slack
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap