Java Encode encodeXML(String input)

Here you can find the source of encodeXML(String input)

Description

Encodes an input string of plain text to it's XML representation.

License

Apache License

Parameter

Parameter Description
input The plain text string to convert

Return

The XML representation of the plain text.

Declaration

public static String encodeXML(String input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * //from   w ww  . ja  va2  s . c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.IOException;

import java.io.StringReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Encodes an input string of plain text to it's XML representation. The
     * special characters &, <, > and " and all characters with character code >=
     * 127 are converted to numeric XML entities.
     * 
     * @param input
     *            The plain text string to convert
     * @return The XML representation of the plain text.
     */
    public static String encodeXML(String input) {

        StringReader in = new StringReader(input);
        StringBuffer out = new StringBuffer();
        int ch;

        try {
            while ((ch = in.read()) != -1) {
                if (ch < 127 && ch != '&' && ch != '<' && ch != '>' && ch != '"') {
                    out.append((char) ch);
                } else {
                    out.append('&').append('#').append(ch).append(';');
                }
            }
        } catch (IOException exc) {
            exc.printStackTrace();
        }

        return out.toString();
    }

    /**
     * Creates a new list that contains the string representations
     * of all elements of the original list
     * null values are preserved.
     * 
     * @param listOriginal
     * @return The list with all elements converted to their string representation
     */
    public static List<String> toString(List<Object> listOriginal) {

        List<String> list = new ArrayList<String>();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem != null) {
                list.add(String.valueOf(elem));
            } else {
                list.add(null);
            }
        }

        return list;

    }
}

Related

  1. encodeUTCTime(long time, OutputStream os)
  2. encodeUtf8AsBase64(String data)
  3. encodeVarUInt32(int unsignedValue, OutputStream outputStream)
  4. encodeVarUInt64(long unsignedValue, OutputStream outputStream)
  5. encodeWikiTag(String s)
  6. encodeXMLString(String aString, PrintWriter aWriter)