Java XML Sanitize SanitizeXmlString(String xml)

Here you can find the source of SanitizeXmlString(String xml)

Description

Sanitize Xml String

License

Open Source License

Declaration

public static String SanitizeXmlString(String xml) 

Method Source Code

//package com.java2s;

public class Main {
    public static String SanitizeXmlString(String xml) {
        if (xml == null || xml.isEmpty()) {
            return xml;
        }/* ww  w.j  a  va 2s  . com*/

        StringBuilder buffer = new StringBuilder(xml.length());

        for (int i = 0; i < xml.length(); i++) {
            if (IsLegalXmlChar(xml.charAt(i))) {
                buffer.append(xml.charAt(i));
            }
        }

        return buffer.toString();
    }

    public static boolean IsLegalXmlChar(int character) {
        return (character == 0x9 /* == '\t' == 9   */ || character == 0xA /* == '\n' == 10  */
                || character == 0xD /* == '\r' == 13  */ || (character >= 0x20 && character <= 0xD7FF)
                || (character >= 0xE000 && character <= 0xFFFD) || (character >= 0x10000 && character <= 0x10FFFF));
    }
}

Related

  1. sanitizeJson4XML(String jsonString)
  2. sanitizeXML(String xml)
  3. sanitizeXmlTypeName(String name)