- Escape XML Content and Attribute in Java using Google Guava
- How to add Google Guava library to the Java project
- How to escape XML content in Java
- How to escape XML attribute value in Java
- Escape XML Content and Attribute in Java using Google Guava
- How to add Google Guava library to the Java project
- How to escape XML content in Java
- How to escape XML attribute value in Java
- How to replace escape XML special characters in Java String — Example
- What is XML and HTML special characters
Escape XML Content and Attribute in Java using Google Guava
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.
com.google.guava guava 30.1.1-jre
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.
import com.google.common.escape.Escaper; import com.google.common.xml.XmlEscapers; public class XmlContentEscaperExample public static void main(String. args) String xmlString = "\n" + " com.google.guava \n" + " guava \n" + " 30.1.1-jre \n" + "\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); > >
XML String: com.google.guava guava 30.1.1-jre Escaped XML String: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency>
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.
import com.google.common.escape.Escaper; import com.google.common.xml.XmlEscapers; public class XmlAttributeEscaperExample public static void main(String. args) String xmlString = " <>"; Escaper escaper = XmlEscapers.xmlAttributeEscaper(); String result = escaper.escape(xmlString); System.out.println("XML String:\n" + xmlString); System.out.println("\nEscaped XML String:\n" + result); > >
XML String: <> Escaped XML String: <simple solution> <>
Or you can shortly escape a string with the below line of code.
String result = XmlEscapers.xmlAttributeEscaper().escape(xmlString);
Escape XML Content and Attribute in Java using Google Guava
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.
com.google.guava guava 30.1.1-jre
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.
import com.google.common.escape.Escaper; import com.google.common.xml.XmlEscapers; public class XmlContentEscaperExample public static void main(String. args) String xmlString = "\n" + " com.google.guava \n" + " guava \n" + " 30.1.1-jre \n" + "\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); > >
XML String: com.google.guava guava 30.1.1-jre Escaped XML String: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency>
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.
import com.google.common.escape.Escaper; import com.google.common.xml.XmlEscapers; public class XmlAttributeEscaperExample public static void main(String. args) String xmlString = " <>"; Escaper escaper = XmlEscapers.xmlAttributeEscaper(); String result = escaper.escape(xmlString); System.out.println("XML String:\n" + xmlString); System.out.println("\nEscaped XML String:\n" + result); > >
XML String: <> Escaped XML String: <simple solution> <>
Or you can shortly escape a string with the below line of code.
String result = XmlEscapers.xmlAttributeEscaper().escape(xmlString);
How to replace escape XML special characters in Java String — Example
There are two approaches to replace XML or HTML special characters from Java String, First, Write your own function to replace XML special characters or use any open source library which has already implemented it. Luckily there is one very common open-source library that provides a function to replace special characters from XML String i s Apache commons lang’s StringEscapeUtils class which provides escaping for several languages like XML, SQL, and HTML. you can use StringEscapeUtils to convert XML special characters in String to their escaped equivalent. I personally like to use open-source code instead of reinventing the wheel to avoid any testing efforts.
Even Joshua Bloch advocated the use of Open source library to leverage the experience and work of other programmers . If you are reading from XML file and after doing some transformation writing to another XML file, you need to take care of XML special characters present in the source file.
If you don’t escape XML special characters while creating XML document than various XML parsers like DOM and SAX parser will consider those XML meta consider them as XML tag in case of < or >.
Even if you try to transform XML with a special character using XSLT transformation, it will complain and fail. So while generating XML documents it’s very important to escape XML special characters to avoid any parsing or transformation issues. In this Java XML tutorial, we will see What is special characters in XML and how to escape XML characters from Java String.
What is XML and HTML special characters
There are five special characters in XML String which require escaping. if you have been working with XML and Java y ou might be familiar with these five characters. Here is a list of XML and HTML special characters :
Sometimes these special characters are also referred as XML metacharacters. For programmers who are not familiar with escaping, escaping is the process to use alternative String in order to produce the literal result of special characters. for example, following XML String is invalid: