Java XML String Create toXMLString(String string)

Here you can find the source of toXMLString(String string)

Description

to XML String

License

Open Source License

Declaration

public static final String toXMLString(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, 2012 Red Hat, Inc. 
 * All rights reserved. //from  www  .j a  v  a  2 s  .  c  o m
 * This program is 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: 
 * Red Hat, Inc. - initial API and implementation 
 *******************************************************************************/

public class Main {
    public static final String toXMLString(String string) {
        if (string == null || string.isEmpty())
            return ""; //$NON-NLS-1$

        StringBuffer xmlString = new StringBuffer();
        int stringLength = string.length();

        for (int i = 0; i < stringLength; i++) {
            char c = string.charAt(i);
            if (c == '"')
                xmlString.append("&quot;"); //$NON-NLS-1$
            else if (c == '&')
                xmlString.append("&amp;"); //$NON-NLS-1$
            else if (c == '\'')
                xmlString.append("&apos;"); //$NON-NLS-1$
            else if (c == '<')
                xmlString.append("&lt;"); //$NON-NLS-1$
            else if (c == '>')
                xmlString.append("&gt;"); //$NON-NLS-1$
            else
                xmlString.append(c);
        }
        return xmlString.toString();
    }
}

Related

  1. toXMLString(String in)
  2. toXMLString(String javaString)
  3. toXMLString(String org)
  4. toXmlString(String s)
  5. toXMLString(String str)
  6. toXMLString(String t)
  7. toXMLString(String text)
  8. toXMLUnescapedText(final String text)