Java XML Encode xmlEncode(String s)

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

Description

Encode string to XML.

License

Open Source License

Parameter

Parameter Description
s string to encode

Return

encoded string

Declaration

public static String xmlEncode(String s) 

Method Source Code

//package com.java2s;
/*/*from w ww.  j  ava2  s .  c o  m*/
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation. For the full
 * license text, see http://www.gnu.org/licenses/lgpl.html.
 */

public class Main {
    /**
     * Encode string to XML.
     * This will encode the following raw characters:
     * <ul>
     * <li>&quot; = &amp;quot;</li>
     * <li>&gt; = &amp;gt;</li>
     * <li>&lt; = &amp;lt;</li>
     * <li>&amp; = &amp;amp;</li>
     * </ul>
     *
     * @param   s   string to encode
     * @return  encoded string
     */
    public static String xmlEncode(String s) {
        return s.replaceAll("&", "&amp;").replaceAll("\"", "&quot;").replaceAll(">", "&gt;").replaceAll("<", "&lt;")
                .replaceAll("'", "&apos;");
    }
}

Related

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