Escape XML Content and Attribute in Java using Google Guava

Tags: Google Guava Escaper XmlEscapers xmlContentEscaper xmlAttributeEscaper XML Escape XML

In this Java tutorial we learn how to escape a XML content string or XML attribute value using the XmlEscapers class of Google Guava library.

How to add Google Guava library to the Java project

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

implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'

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

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>

To have more information about the Google Guava library you can visit the project home page at guava.dev

How to escape XML content in Java

In the following Java example program, we show you how to use the XmlEscapers class of Google Guava library to encode a given XML string, which escapes special characters in the string so it can safely be included in an XML document as element content.

XmlContentEscaperExample.java

import com.google.common.escape.Escaper;
import com.google.common.xml.XmlEscapers;

public class XmlContentEscaperExample {
    public static void main(String... args) {
        String xmlString = "<dependency>\n" +
                "    <groupId>com.google.guava</groupId>\n" +
                "    <artifactId>guava</artifactId>\n" +
                "    <version>30.1.1-jre</version>\n" +
                "</dependency>\n";

        Escaper escaper = XmlEscapers.xmlContentEscaper();
        String result = escaper.escape(xmlString);

        System.out.println("XML String:\n" + xmlString);
        System.out.println("\nEscaped XML String:\n" + result);
    }
}
The output is:
XML String:
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>


Escaped XML String:
&lt;dependency&gt;
    &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
    &lt;artifactId&gt;guava&lt;/artifactId&gt;
    &lt;version&gt;30.1.1-jre&lt;/version&gt;
&lt;/dependency&gt;

Or you can shortly escape a string with the below line of code.

String result = XmlEscapers.xmlContentEscaper().escape(xmlString);

How to escape XML attribute value in Java

In the following Java example program, we show you how to use the XmlEscapers class of Google Guava library to encode a given string, which escapes special characters in the string so it can safely be included in an XML document as an attribute value.

XmlAttributeEscaperExample.java

import com.google.common.escape.Escaper;
import com.google.common.xml.XmlEscapers;

public class XmlAttributeEscaperExample {
    public static void main(String... args) {
        String xmlString = "<simple solution> <>";

        Escaper escaper = XmlEscapers.xmlAttributeEscaper();
        String result = escaper.escape(xmlString);

        System.out.println("XML String:\n" + xmlString);
        System.out.println("\nEscaped XML String:\n" + result);
    }
}
The output is:
XML String:
<simple solution> <>

Escaped XML String:
&lt;simple solution&gt; &lt;&gt;

Or you can shortly escape a string with the below line of code.

String result = XmlEscapers.xmlAttributeEscaper().escape(xmlString);

Happy Coding 😊