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

StringxmlEncode(String string)
xml Encode
if (string.equals("")) { 
    return string;
int index = string.indexOf('&');
while (index != -1) {
    string = string.substring(0, index) + "&" 
            + string.substring(index + 1);
    index = string.indexOf('&', index + 1);
...
StringxmlEncode(String text)
xml Encode
int length = text.length();
if (text != null && length > 0) {
    StringBuffer ret = new StringBuffer(length * 12 / 10);
    int last = 0;
    for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        switch (c) {
        case '&':
...
StringxmlEncode(String text)
xml Encode
return xmlEncode(text, null);
StringxmlEncode(String text)
Encodes the text into safe XML by replacing < > and & with XML tokens
if (text == null) {
    return "";
text = text.replaceAll("&", "&amp;");
text = text.replaceAll("\"", "&quot;");
text = text.replaceAll("<", "&lt;");
text = text.replaceAll(">", "&gt;");
return text;
...
StringxmlEncode(String text, String invalidCharReplacement)
xml Encode
if (text == null || text.length() == 0) {
    return text;
int length = text.length();
StringBuffer ret = new StringBuffer();
int last = 0;
for (int i = 0; i < length; i++) {
    char c = text.charAt(i);
...
StringXMLEncode(String value)
XML Encode
if (value == null)
    return value;
return value.replaceAll("&", "&amp;").replaceAll("'", "&apos;").replaceAll("\"", "&quot;")
        .replaceAll("<", "&lt;").replaceAll(">", "&gt;");
StringxmlEncodeAttr(String src)
xml Encode Attr
StringBuffer sb = new StringBuffer(src.length());
for (int i = 0; i < src.length(); i++) {
    char ch = src.charAt(i);
    switch (ch) {
    case '&':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&amp;");
        break;
    case '<':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&lt;");
        break;
    case '>':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&gt;");
        break;
    case '\'':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&apos;");
        break;
    case '\"':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&quot;");
        break;
    case '\r':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&#13;");
        break;
    case '\n':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&#10;");
        break;
    case '\t':
        if (sb == null) {
            sb = createStringBuffer(src, i);
        sb.append("&#9;");
        break;
    default:
        if (sb != null) {
            sb.append(ch);
return sb != null ? sb.toString() : src;
StringxmlEncodeCDATA(String src)
xml Encode CDATA
return xmlEncodeCDATA(src, false);