Java Utililty Methods XML Sanitize

List of utility methods to do XML Sanitize

Description

The list of methods to do XML Sanitize are organized into topic(s).

Method

StringsanitizeJson4XML(String jsonString)
sanitizeJson4XML - Sanitize a JSON string so it converts to XML without error.
jsonString = jsonString.trim();
if (!jsonString.startsWith("{")) {
    jsonString = "{\"Anonymous\":" + jsonString + "}";
return jsonString;
StringsanitizeXML(String xml)
Remove all unnecessary white spaces, tabs and line breaks from XML string.
return xml.replace("\n", "").replace("\r", "").replace("\t", "").replaceAll(">\\s+<", "><").trim();
StringSanitizeXmlString(String xml)
Sanitize Xml String
if (xml == null || xml.isEmpty()) {
    return xml;
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();
StringsanitizeXmlTypeName(String name)
Since XML names can not contain all characters JSON names can, we may need to replace characters.
StringBuilder sb;
int changes = 0;
if (name.endsWith("[]")) {
    do {
        name = name.substring(0, name.length() - 2);
        ++changes;
    } while (name.endsWith("[]"));
    sb = new StringBuilder(name);
...