Java tutorial
//package com.java2s; public class Main { public static String filterInvalid(String xml) { StringBuffer sb = new StringBuffer(xml); for (int i = 0; i < sb.length(); i++) { int c = sb.charAt(i); if (c < 0x20 && c != 0x09/*\t*/ && c != 0x0A/*\n*/ && c != 0x0D/*\r*/) { sb.delete(i, i + 1); } } return sb.toString(); } }