Java XML Encode textToXml(String text)

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

Description

Given text strings containing xml reserved characters, replace with valid xml representation characters > => & gt; < => & lt; & => & amp; ' => & apos; " => & quot;

License

Open Source License

Parameter

Parameter Description
text text to be converted to valid XML representation characters

Declaration

public static String textToXml(String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing./*w ww  .jav  a2s.c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Given text strings containing xml reserved characters, replace with valid xml representation characters > => & gt;
     * < => & lt; & => & amp; ' => & apos; " => & quot;
     *
     * @param text text to be converted to valid XML representation characters
     */
    public static String textToXml(String text) {
        if (text == null || text.equals("")) {
            return "";
        }
        String str = text;
        str = str.replaceAll("&", "&amp;");
        str = str.replaceAll(">", "&gt;");
        str = str.replaceAll("<", "&lt;");
        str = str.replaceAll("'", "&apos;");
        str = str.replaceAll("\"", "&quot;");
        return str;
    }
}

Related

  1. encodeXML(String text)
  2. textToXML(String text)
  3. toXMLCData(String cdata)
  4. toXMLCharData(String javaString)
  5. toXMLEscapedTextExpandingWhitespace(String text)