public static boolean isValidXMLString ( String s ) { int length = s . length ( ) ; // Loop through the string by UNICODE codepoints. This is NOT equivalent // to looping through the characters because some UNICODE codepoints can // occupy more than one character. int index = 0 ; while ( index < length ) { int codePoint = s . codePointAt ( index ) ; if ( ! isValidXMLCharacter ( codePoint ) ) { return false ; } index += Character . charCount ( codePoint ) ; } // If we get here then all of the characters have been checked and are // valid. Unfortunately the usual case takes the longest to verify. return true ; }
Determine if the given string can be written to an XML file without encoding . This will be the case so long as the string does not contain illegal XML characters .