Java XML Encode xmlEncode(String string)

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

Description

xml Encode

License

Open Source License

Declaration

public static final String xmlEncode(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 Remy Suen//from   w ww  .  j ava 2s.  c om
 * 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:
 *    Remy Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/

public class Main {
    public static final String xmlEncode(String string) {
        if (string.equals("")) { //$NON-NLS-1$
            return string;
        }

        int index = string.indexOf('&');
        while (index != -1) {
            string = string.substring(0, index) + "&amp;" //$NON-NLS-1$
                    + string.substring(index + 1);
            index = string.indexOf('&', index + 1);
        }

        index = string.indexOf('"');
        while (index != -1) {
            string = string.substring(0, index) + "&quot;" //$NON-NLS-1$
                    + string.substring(index + 1);
            index = string.indexOf('"', index + 1);
        }

        index = string.indexOf('\'');
        while (index != -1) {
            string = string.substring(0, index) + "&apos;" //$NON-NLS-1$
                    + string.substring(index + 1);
            index = string.indexOf('\'', index + 1);
        }

        index = string.indexOf('<');
        while (index != -1) {
            string = string.substring(0, index) + "&lt;" //$NON-NLS-1$
                    + string.substring(index + 1);
            index = string.indexOf('<', index + 1);
        }

        index = string.indexOf('>');
        while (index != -1) {
            string = string.substring(0, index) + "&gt;" //$NON-NLS-1$
                    + string.substring(index + 1);
            index = string.indexOf('>', index + 1);
        }
        return string;
    }
}

Related

  1. xmlEncode(String s)
  2. xmlEncode(String s)
  3. xmlEncode(String s)
  4. xmlEncode(String str)
  5. xmlEncode(String str)
  6. xmlEncode(String text)
  7. xmlEncode(String text)
  8. xmlEncode(String text)
  9. xmlEncode(String text, String invalidCharReplacement)