replace Non XML Characters - Java XML

Java examples for XML:XML String

Description

replace Non XML Characters

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String text = "java2s.com";
        System.out.println(replaceNonXMLCharacters(text));
    }//from  w w w.  j  a  v  a2s . c o  m

    public static String replaceNonXMLCharacters(String text) {
        if (text == null) {
            return null;
        }

        StringBuffer sb = new StringBuffer(text);
        for (int i = 0, len = text.length(); i < len; i++) {
            if (!isXMLCharacter(text.charAt(i))) {
                sb.setCharAt(i, '?');
            }
        }
        return sb.toString();
    }

    /**
     * This is a utility function for determining whether a specified 
     * character is a character according to production 2 of the 
     * XML 1.0 specification.
     *  
     * <p>Copied from org.jdom.Verifier.
     * 
     * <p>Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
     * All rights reserved.
     *
     * @param c <code>char</code> to check for XML compliance
     * @return <code>boolean</code> true if it's a character, 
     *                                false otherwise
     */
    public static boolean isXMLCharacter(char c) {

        if (c == '\n')
            return true;
        if (c == '\r')
            return true;
        if (c == '\t')
            return true;

        if (c < 0x20)
            return false;
        if (c <= 0xD7FF)
            return true;
        if (c < 0xE000)
            return false;
        if (c <= 0xFFFD)
            return true;
        if (c < 0x10000)
            return false;
        if (c <= 0x10FFFF)
            return true;

        return false;
    }
}

Related Tutorials