Pretty Print XML String and XML File in Java using jsoup

In this tutorial, we are going to show how to use the jsoup library in a Java program to pretty sprint a XML String or XML file content. We provide different working Java programs to show different use cases on how to parse XML String or XML File into jsoup Document object in order to format the XML content for pretty printing.

Add jsoup library to your Java project

To use jsoup Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile 'org.jsoup:jsoup:1.13.1'

To use jsoup Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

To download the jsoup-1.13.1.jar file you can visit jsoup download page at jsoup.org/download

Pretty Print XML String in Java

In the following Java code example we convert a XML String to pretty print format.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

public class PrettyPrintXmlStringExample {
    public static void main(String... args) {
        String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><customers><customer><firstName>aaa</firstName><lastName>bbb</lastName></customer><customer><firstName>ccc</firstName><lastName>ddd</lastName></customer></customers>";

        Document document = Jsoup.parse(xmlContent, "", Parser.xmlParser());

        String formattedXmlString = document.toString();

        System.out.println(formattedXmlString);
    }
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
 <customer>
  <firstName>
   aaa
  </firstName>
  <lastName>
   bbb
  </lastName>
 </customer>
 <customer>
  <firstName>
   ccc
  </firstName>
  <lastName>
   ddd
  </lastName>
 </customer>
</customers>

Pretty Print XML File in Java

In the following Java code example we read an XML file and create a new file with pretty print format content.

For example, we have data.xml file with following content.

<?xml version="1.0" encoding="UTF-8"?><customers><customer><firstName>aaa</firstName><lastName>bbb</lastName></customer><customer><firstName>ccc</firstName><lastName>ddd</lastName></customer></customers>

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PrettyPrintXmlFileExample {
    public static void main(String... args) {
        try {
            File file = new File("data.xml");
            FileInputStream fileInputStream = new FileInputStream(file);

            Document document = Jsoup.parse(fileInputStream, "UTF-8", "", Parser.xmlParser());

            String formattedXmlString = document.toString();

            Path outputFilePath = Paths.get("data_output.xml");
            Files.write(outputFilePath, formattedXmlString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The data_output.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<customers>
 <customer>
  <firstName>
   aaa
  </firstName>
  <lastName>
   bbb
  </lastName>
 </customer>
 <customer>
  <firstName>
   ccc
  </firstName>
  <lastName>
   ddd
  </lastName>
 </customer>
</customers>

Happy Coding 😊

Pretty Printing HTML String in Java using jsoup