Java XML Encode xmlEncode(String text)

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

Description

Encodes the text into safe XML by replacing < > and & with XML tokens

License

Apache License

Parameter

Parameter Description
text the text

Return

the encoded text

Declaration

public static String xmlEncode(String text) 

Method Source Code

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

public class Main {
    /**//from   w w  w. j a va 2  s  .  co  m
     * Encodes the text into safe XML by replacing < > and & with XML tokens
     *
     * @param text  the text
     * @return the encoded text
     */
    public static String xmlEncode(String text) {
        if (text == null) {
            return "";
        }
        // must replace amp first, so we don't replace &lt; to amp later
        text = text.replaceAll("&", "&amp;");
        text = text.replaceAll("\"", "&quot;");
        text = text.replaceAll("<", "&lt;");
        text = text.replaceAll(">", "&gt;");
        return text;
    }
}

Related

  1. xmlEncode(String str)
  2. xmlEncode(String str)
  3. xmlEncode(String string)
  4. xmlEncode(String text)
  5. xmlEncode(String text)
  6. xmlEncode(String text, String invalidCharReplacement)
  7. XMLEncode(String value)
  8. xmlEncodeAttr(String src)
  9. xmlEncodeCDATA(String src)