How to supercede XML exceptional Characters inward Java String
There are 2 approaches to supercede XML or HTML exceptional characters from Java String, First, Write your ain component to supercede XML exceptional characters or operate whatsoever opened upwardly beginning library which has already implemented it. Luckily in that location is ane really mutual opened upwardly beginning library which provides a component to supercede exceptional characters from XML String is Apache common lang’s StringEscapeUtils class which furnish escaping for several languages similar XML, SQL, in addition to HTML. yous tin operate StringEscapeUtils to convert XML exceptional grapheme inward String to their escaped equivalent. I personally similar to operate opened upwardly beginning code instead of reinventing the bike to avoid whatsoever testing efforts. Even Joshua Bloch equally advocated operate of Open beginning library to leverage the sense in addition to piece of job of other programmers. If yous are reading from XML file and afterward doing unopen to transformation writing to unopen to other XML file , yous quest to receive got attention of XML exceptional characters acquaint inward the beginning file. If yous don’t escape XML exceptional characters spell creating XML document than diverse XML parsers similar DOM in addition to SAX parser volition see those XML meta see them equally XML tag inward instance of < or >.
Even if yous assay to transform XML amongst a exceptional grapheme using XSLT transformation, it volition complain in addition to fail. So spell generating XML documents it's really of import to escape XML exceptional characters to avoid whatsoever parsing or transformation issues. In this Java XML tutorial, nosotros volition meet What is exceptional characters inward XML and how to escape XML characters from Java String.
Even if yous assay to transform XML amongst a exceptional grapheme using XSLT transformation, it volition complain in addition to fail. So spell generating XML documents it's really of import to escape XML exceptional characters to avoid whatsoever parsing or transformation issues. In this Java XML tutorial, nosotros volition meet What is exceptional characters inward XML and how to escape XML characters from Java String.
What is XML in addition to HTML exceptional characters
There are 5 exceptional characters inward XML String which require escaping. if yous receive got been working with XML in addition to Java you powerfulness last familiar amongst these 5 characters. Here is a list of XML in addition to HTML exceptional characters :
- & - &
- < - <
- > - >
- " - "
- ' - '
String in lodge to attain the literal number of exceptional characters. for example, next XML String is invalid:
<languages>Java & HTML</languages>
because & grapheme is used to import other XML entity. In lodge to operate & grapheme equally XML or String literal nosotros quest to operate &, only similar shown in
below example:
<languages>Java & HTML</languages>
Similarly, if yous desire to operate higher upwardly 5 exceptional XML characters equally String literal then yous quest to escape those. Even spell writing these post if I don’t escape these HTML exceptional character, they volition last considered equally HTML tag past times HTML parser. In lodge to present them equally it is, I quest to escape these XML exceptional characters.
Code instance to supercede XML Special characters inward String.
Here is consummate code instance to supercede exceptional characters inward XML string. This instance uses StringEscapeUtils from Apache common to perform escaping:
import org.apache.commons.lang.StringEscapeUtils;
/**
* Simple Java programme to escape XML or HTML exceptional characters inward String.
* There are 5 XML Special characters which needs to last escaped :
* & - &
< - <
> - >
" - "
' - '
* @author http://javarevisited.blogspot.com
*/
public class XMLUtils {
public static void main(String args[]) {
//handling xml exceptional grapheme & inward Java String
String xmlWithSpecial = "Java & HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String inward Java: "
+ StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling xml exceptional grapheme > inward String on Java
xmlWithSpecial = "Java > HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String : " + StringEscapeUtils.escapeXml(xmlWithSpecial));
/**
* Simple Java programme to escape XML or HTML exceptional characters inward String.
* There are 5 XML Special characters which needs to last escaped :
* & - &
< - <
> - >
" - "
' - '
* @author http://javarevisited.blogspot.com
*/
public class XMLUtils {
public static void main(String args[]) {
//handling xml exceptional grapheme & inward Java String
String xmlWithSpecial = "Java & HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String inward Java: "
+ StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling xml exceptional grapheme > inward String on Java
xmlWithSpecial = "Java > HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String : " + StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling xml in addition to html exceptional grapheme < inward String
xmlWithSpecial = "Java < HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
xmlWithSpecial = "Java < HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling html in addition to xml exceptional grapheme " inward Java
xmlWithSpecial = "Java \" HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling xml exceptional grapheme ' inward String from Java
xmlWithSpecial = "Java ' HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
}
}
Output
Original unescaped XML String: Java & HTML
Escaped XML String inward Java: Java & HTML
Original unescaped XML String: Java > HTML
Escaped XML String : Java > HTML
Original unescaped XML String: Java < HTML
Escaped XML String: Java < HTML
Original unescaped XML String: Java " HTML
Escaped XML String: Java " HTML
Original unescaped XML String: Java ' HTML
Escaped XML String: Java ' HTML
xmlWithSpecial = "Java \" HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
//handling xml exceptional grapheme ' inward String from Java
xmlWithSpecial = "Java ' HTML"; //xml String amongst & equally exceptional characters
System.out.println("Original unescaped XML String: " + xmlWithSpecial);
System.out.println("Escaped XML String: " + StringEscapeUtils.escapeXml(xmlWithSpecial));
}
}
Output
Original unescaped XML String: Java & HTML
Escaped XML String inward Java: Java & HTML
Original unescaped XML String: Java > HTML
Escaped XML String : Java > HTML
Original unescaped XML String: Java < HTML
Escaped XML String: Java < HTML
Original unescaped XML String: Java " HTML
Escaped XML String: Java " HTML
Original unescaped XML String: Java ' HTML
Escaped XML String: Java ' HTML
That’s all on how to escape XML Special characters on Java program. It's ane of the principal displace of põrnikas spell working amongst XML parsing in addition to transformation inward Java and proper treatment of XML in addition to HTML exceptional characters are required. If yous are using a database to shop your XML in addition to then see storing escaped XML instead of raw XML, this volition ensure that every customer read XML from the database volition receive got proper escaped XML or HTML.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services in addition to REST API amongst Spring Boot
My notes on dealing amongst XPATH inward Java