Tuesday, May 14, 2024
HomeJavaHow one can change escape XML particular characters in Java String

How one can change escape XML particular characters in Java String


Disclosure: This text might include affiliate hyperlinks. If you buy, we might earn a small fee.

How one can change XML particular Characters in Java String

There are two approaches to switch XML or HTML particular characters from Java String, First,  Write your individual perform to switch XML particular characters or use any open supply library which has already applied it. Fortunately there’s one quite common open-source library that gives a perform to switch particular characters from XML String is Apache commons lang’s StringEscapeUtils class which offers escaping for a number of languages like XML, SQL, and HTML. you should use StringEscapeUtils to convert XML particular characters in String to their escaped equal. I personally like to make use of open-source code as a substitute of reinventing the wheel to keep away from any testing efforts.


Even Joshua Bloch advocated the usage of Open supply library to leverage the expertise and work of different programmers. If you’re
studying from XML file and after performing some transformation writing to a different XML file, you could maintain XML particular characters current within the supply file. 


In case you don’t escape XML particular characters whereas creating XML doc than varied XML parsers like DOM and SAX parser will contemplate these XML meta contemplate them as XML tag in case of < or >. 

Even in case you attempt to remodel XML with a particular character utilizing  XSLT transformation, it’ll complain and fail. So whereas producing XML paperwork it is essential to flee XML particular characters to keep away from any parsing or transformation points. On this Java XML tutorial, we’ll see What’s particular characters in XML and the way to escape XML characters from Java String.

How to replace escape XML special characters in Java String - Example

What’s XML and HTML particular characters

There are 5 particular characters in XML String which require escaping. when you’ve got been working with XML and Java you is perhaps accustomed to these 5 characters. Here’s a record of XML and HTML particular characters :
  1.  & – &amp;
  2.  < – &lt;
  3.  > – &gt;
  4.  ” – &quot;
  5.  ‘ – &apos;
escape XML special characters in Java program exampleTypically these particular characters are additionally referred as XML metacharacters. For programmers who usually are not accustomed to escaping, escaping is the method to make use of various String in order to provide the literal results of particular characters. for instance, following XML String is invalid:

<languages>Java & HTML</languages>

as a result of & character is used to import different XML entity. To be able to use & character as XML or String literal we have to use &amp;, similar to proven in

beneath instance:

<languages>Java &amp; HTML</languages>

Equally, if you wish to use above 5 particular XML characters as String literal then you could escape these. Even whereas writing these posts if I don’t escape these  HTML particular characters, they are going to be thought of as HTML tags by HTML parser. To be able to present them as it’s, I have to escape these XML particular characters.

Code instance to switch XML Particular characters in String.

Here’s a full code instance to switch particular characters in an XML string. This instance makes use of StringEscapeUtils from Apache commons to carry out escaping:

import org.apache.commons.lang.StringEscapeUtils;

/**
 * Easy Java program to flee XML or HTML particular characters in String.
 * There are 5 XML Particular characters which must be escaped :
 *     & – &amp;
    < – &lt;
    > – &gt;
    ” – &quot;
    ‘ – &apos;
 * @writer http://javarevisited.blogspot.com
 */

public class XMLUtils {
 
 
    public static void predominant(String args[]) {
   
        //dealing with xml particular character & in Java String
        String xmlWithSpecial = “Java & HTML”; //xml String with & as particular characters
        System.out.println(“Unique unescaped XML String: “ + xmlWithSpecial);
        System.out.println(“Escaped XML String in Java: “
                            +  StringEscapeUtils.escapeXml(xmlWithSpecial));
     
        //dealing with xml particular character > in String on Java
        xmlWithSpecial = “Java > HTML”; //xml String with & as particular characters
        System.out.println(“Unique unescaped XML String: “ + xmlWithSpecial);
        System.out.println(“Escaped XML String : “ + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
       

        //dealing with xml and html particular character < in String
        xmlWithSpecial = “Java < HTML”; //xml String with & as particular characters
        System.out.println(“Unique unescaped XML String: “ + xmlWithSpecial);
        System.out.println(“Escaped XML String: “ + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
       

        //dealing with html and xml particular character ” in Java
        xmlWithSpecial = “Java HTML”; //xml String with & as particular characters
        System.out.println(“Unique unescaped XML String: “ + xmlWithSpecial);
        System.out.println(“Escaped XML String: “ + StringEscapeUtils.escapeXml(xmlWithSpecial));
     
        //dealing with xml particular character ‘ in String from Java
        xmlWithSpecial = “Java ‘ HTML”; //xml String with & as particular characters
        System.out.println(“Unique unescaped XML String: “ + xmlWithSpecial);
        System.out.println(“Escaped XML String: “ + StringEscapeUtils.escapeXml(xmlWithSpecial));
   
    }
 
}

Output
Unique unescaped XML String: Java & HTML
Escaped XML String in Java: Java &amp; HTML
Unique unescaped XML String: Java > HTML
Escaped XML String : Java &gt; HTML
Unique unescaped XML String: Java < HTML
Escaped XML String: Java &lt; HTML
Unique unescaped XML String: Java ” HTML
Escaped XML String: Java &quot; HTML
Unique unescaped XML String: Java ‘ HTML
Escaped XML String: Java &apos; HTML

That’s all on the way to escape XML Particular characters on Java program. It is one of many predominant causes of bug whereas working with XML parsing and transformation in Java and correct dealing with of XML and HTML particular characters are required. If you’re utilizing a database to retailer your XML then contemplate storing escaped XML as a substitute of uncooked XML, this may be certain that each consumer learn XML from the database could have correct escaped XML or HTML.

Different Java and XML tutorials from Javarevisited Weblog



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments