Java Utililty Methods XML Encode

List of utility methods to do XML Encode

Description

The list of methods to do XML Encode are organized into topic(s).

Method

StringencodeXML(String text)
Escape characters for text appearing as XML data, between tags.
if (text == null) {
    return null;
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(text);
char character = iterator.current();
while (character != CharacterIterator.DONE) {
    if (character == '<') {
...
StringtextToXml(String text)
Given text strings containing xml reserved characters, replace with valid xml representation characters > => & gt; < => & lt; & => & amp; ' => & apos; " => & quot;
if (text == null || text.equals("")) {
    return "";
String str = text;
str = str.replaceAll("&", "&amp;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("<", "&lt;");
str = str.replaceAll("'", "&apos;");
...
StringtextToXML(String text)
Converts a text string to XML entities.
return textToHTML(text, true);
StringtoXMLCData(String cdata)
to XMLC Data
char[] charArray = cdata.toCharArray();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
    final char ch = charArray[i];
    switch (cdataFilter(ch)) {
    case 0:
        buffer.append(ch);
        break;
...
StringtoXMLCharData(String javaString)
to XML Char Data
char[] charArray = javaString.toCharArray();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
    final char ch = charArray[i];
    switch (ch) {
    case ' ':
        buffer.append(' ');
        break;
...
StringtoXMLEscapedTextExpandingWhitespace(String text)
to XML Escaped Text Expanding Whitespace
text = text.replaceAll("\t", "         ");
final int len = text.length();
final StringBuilder result = new StringBuilder(len);
char myChar;
for (int i = 0; i < len; ++i) {
    myChar = text.charAt(i);
    switch (myChar) {
    case '&':
...
StringunwrapCdata(String s)
unwrap Cdata
return (s.startsWith("<![CDATA[") && s.endsWith("]]>")
        ? s.substring(9, s.length() - 3).replace("]]]]><![CDATA[>", "]]>")
        : s);
StringunwrapCdata(String sText)
If the text is wrapped with a "[CDATA[ ]]" tag then it is removed and the text inside is returned.
if ((sText != null) && (sText.trim().startsWith("[CDATA[")) && (sText.trim().endsWith("]]"))) {
    return sText.trim().substring(7, sText.trim().length() - 2);
} else {
    return sText;
StringunwrapCDATA(String str)
unwrap CDATA
return str.replace(CDATA_START, "").replace(CDATA_END, "");
StringxmlEncode(final String text)
xml Encode
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
    char currentChar = text.charAt(i);
    if (!((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z')
            || (currentChar >= '0' && currentChar <= '9'))) {
        result.append("&#" + (int) currentChar + ";");
    } else {
        result.append(currentChar);
...