Java XML Encode XMLEncode(final String value)

Here you can find the source of XMLEncode(final String value)

Description

encodes a value for safe use in XML

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration


public static String XMLEncode(final String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  w  ww . j  a  v a  2 s .  co m*/
     *  encodes a value for safe use in XML
     * 
     *  @param value
     *  @return
     * 
     */

    public static String XMLEncode(final String value) {

        String newValue = value.toString();
        final String[] reps = new String[] { "&", "&amp;", "<", "&lt;", ">", "&gt;", "\"", "&quot;", "'",
                "&apos;" };

        for (int i = 0; i < reps.length; i += 2)
            newValue = newValue.replace(reps[i], reps[i + 1]);

        return newValue;

    }
}

Related

  1. toXMLEscapedTextExpandingWhitespace(String text)
  2. unwrapCdata(String s)
  3. unwrapCdata(String sText)
  4. unwrapCDATA(String str)
  5. xmlEncode(final String text)
  6. xmlEncode(String in)
  7. XMLEncode(String name)
  8. xmlEncode(String s)
  9. xmlEncode(String s)