Java XML Encode xmlEncode(String str)

Here you can find the source of xmlEncode(String str)

Description

Replaces &, <, >, " in a string with their respective XML representation

License

Apache License

Parameter

Parameter Description
str the string to encode

Return

a "save" encoded xml string (i.E. for usage in xml attributes)

Declaration

public static String xmlEncode(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w w  w . ja v  a  2s .c  o  m*/
     * Replaces &, <, >, " in a string with their respective XML representation
     *
     * @param str
     *            the string to encode
     * @return a "save" encoded xml string (i.E. for usage in xml attributes)
     */
    public static String xmlEncode(String str) {
        if (str == null)
            return null;
        str = str.replace("&", "&amp;");
        str = str.replace("<", "&lt;");
        str = str.replace(">", "&gt;");
        str = str.replace("\"", "&quot;");
        return str;
    }
}

Related

  1. xmlEncode(String s)
  2. xmlEncode(String s)
  3. xmlEncode(String s)
  4. xmlEncode(String s)
  5. xmlEncode(String str)
  6. xmlEncode(String string)
  7. xmlEncode(String text)
  8. xmlEncode(String text)
  9. xmlEncode(String text)