Replace invalid XML chars by spaces. - Java XML

Java examples for XML:XML Encoding

Description

Replace invalid XML chars by spaces.

Demo Code

/* *************************************************************************
 *
 *  TMPotter - Bi-text Aligner/TMX Editor
 *
 *  Copyright (C) 2015 Hiroshi Miura/*from  w  ww. j  av  a2s.c  o m*/
 *
 *  Part of this come from OmegaT.
 *
 *  Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
 *
 *  This file is part of TMPotter.
 *
 *  TMPotter is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  TMPotter is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with TMPotter.  If not, see http://www.gnu.org/licenses/.
 *
 * *************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        System.out.println(fixChars(str));
    }

    /**
     *  Replace invalid XML chars by spaces. See supported chars at
     *  http://www.w3.org/TR/2006/REC-xml-20060816/#charsets.
     *
     *  @param str input stream
     *  @return result stream
     */
    final public static String fixChars(final String str) {
        char[] result = new char[str.length()];

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (c < 0x20) {
                if (c != 0x09 && c != 0x0A && c != 0x0D)
                    c = ' ';
            } else if (c >= 0x20 && c <= 0xD7FF) {
            } else if (c >= 0xE000 && c <= 0xFFFD) {
            } else if (c >= 0x10000 && c <= 0x10FFFF) {
            } else
                c = ' ';

            result[i] = c;
        }

        return (new String(result));
    }
}

Related Tutorials