Java Char Array to String charArrayToXml(char[] input, int offset, int length)

Here you can find the source of charArrayToXml(char[] input, int offset, int length)

Description

Converts a subset of a character array to XML string format.

License

Open Source License

Parameter

Parameter Description
input the character array to convert
offset offset of the first character in the array subset to convert.
length the number of characters in the array subset.

Return

the XML representation of the character array subset.

Declaration

public static String charArrayToXml(char[] input, int offset, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w ww.  ja  v a 2s.c o  m
     * Converts a subset of a character array to XML string format.
     * @param input the character array to convert
     * @param offset offset of the first character in the array subset to convert.
     * @param length the number of characters in the array subset.
     * @return the XML representation of the character array subset.
     */
    public static String charArrayToXml(char[] input, int offset, int length) {
        StringBuffer buf = new StringBuffer();
        if (length < 0) {
            length = input.length;
        } else {
            length = offset + length;
        }
        if (length > input.length) {
            length = input.length;
        }
        for (int i = offset; i < length; i++) {
            char c = input[i];
            if (c < 32 || c > 126) {
                buf.append(String.format("&#x%02X;", (int) c));
            } else {
                switch (c) {
                case '&':
                    buf.append("&amp;");
                    break;
                case '<':
                    buf.append("&lt;");
                    break;
                case '>':
                    buf.append("&gt;");
                    break;
                case '"':
                    buf.append("&quot;");
                    break;
                case '\'':
                    buf.append("&apos;");
                    break;
                default:
                    buf.append(c);
                    break;
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. charArrayToCharSequence( final char[] charArray)
  2. charArrayToString(char[] ch, String separator)
  3. charArrayToString(char[] input, int offset, int length, int maxChars)
  4. charArrayToString(Character[] chars)
  5. charsToString(char[] chars)
  6. charsToString(char[] chars)