Android Utililty Methods XML String Escape

List of utility methods to do XML String Escape

Description

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

Method

StringtoXMLString(String s)
to XML String
StringBuffer stringbuffer = new StringBuffer();
for (int i = 0; s != null && i < s.length(); i++) {
    char c = s.charAt(i);
    if (c == '\'')
        stringbuffer.append("&#39;");
    else if (c == '"')
        stringbuffer.append("&#34;");
    else if (c == '\n')
...
StringxmlEscape(String xml)
xml Escape
return xml.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;");
Stringescape(String input)
Useful to replace the characters forbidden in xml by their escaped counterparts Ex: & -> &amp;
return input.replace("&", "&amp;").replace("<", "&lt;")
        .replace(">", "&gt;").replace("\"", "&quot;")
        .replace("\'", "&apos;");
StringescapeText(String rawText)
Escapes an XML string (i.e.
StringBuilder escapedText = new StringBuilder();
StringCharacterIterator characterIterator = new StringCharacterIterator(
        rawText);
char currentCharacter = characterIterator.current();
while (currentCharacter != CharacterIterator.DONE) {
    switch (currentCharacter) {
    case '<':
        escapedText.append("&lt;");
...
StringgetXmlEncoded(String text)
get Xml Encoded
StringBuffer buf = new StringBuffer(text);
for (int i = 0; i < buf.length(); ++i) {
    char c = buf.charAt(i);
    if (c == '&') {
        buf.replace(i, i + 1, "&amp;");
    } else if (c == '<') {
        buf.replace(i, i + 1, "&lt;");
    } else if (c == '>') {
...
StringencodeEntities(String content)
encode Entities
content = content.replace("&", "&amp;");
content = content.replace("<", "&lt;");
content = content.replace(">", "&gt;");
content = content.replace("\"", "&quot;");
content = content.replace("'", "&apos;");
return content;
StringencodeEntities(String content)
encode Entities
content = content.replace("&", "&amp;");
content = content.replace("<", "&lt;");
content = content.replace(">", "&gt;");
content = content.replace("\"", "&quot;");
content = content.replace("'", "&apos;");
return content;
StringencodeXML(final String str)
encode XML
final int max = str.length();
final StringBuffer buf = new StringBuffer(max + 10);
for (int i = 0; i < max; ++i)
    final char c = str.charAt(i);
    switch (c) {
    case '&':
        buf.append("&amp;");
...
StringXmlEscape(String value)
Xml Escape
if (value == null)
    return null;
value = value.replaceAll("&", "&amp;");
value = value.replaceAll("<", "&lt;");
value = value.replaceAll(">", "&gt;");
return value;
StringxmlEscape(String source)
Escape a string for use in XML.
StringBuilder sb = new StringBuilder(source.length()
        + (source.length() / 10));
for (int i = 0; i < source.length(); i++) {
    char c = source.charAt(i);
    switch ("<>&'\"".indexOf(c)) {
    case 0:
        sb.append("&lt;");
        break;
...