I am taking some strings from a .xlsx file (the strings are simple characters). Then I am trying to put those strings in an .xml file. But unfortunately, when I put those strings in the ‘createElement(StringVariableHere)’ method, I get the following error: "org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified."
I get the string values in this way:
switch (tempCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
String tempColValue = tempCell.getStringCellValue();
}
And this is the line where i try to add my string values and it gives me the error.
Element titleChild = doc.createElement(StringVariableHere);
I even tried to clean the string using the following method i found online:
public String stripNonValidXMLCharacters(String in) {
StringBuffer out = new StringBuffer(); // Used to hold the output.
char current; // Used to reference the current character.
if (in == null || ("".equals(in))) return ""; // vacancy test.
for (int i = 0; i < in.length(); i++) {
current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
if ((current == 0x9) ||
(current == 0xA) ||
(current == 0xD) ||
((current >= 0x20) && (current <= 0xD7FF)) ||
((current >= 0xE000) && (current <= 0xFFFD)) ||
((current >= 0x10000) && (current <= 0x10FFFF)))
out.append(current);
}
return out.toString();
}
Also i am using the following to check if it is valid or not, and when i add my string it returns false:
XMLChar.isValidName(StringVariableHere)
Thank you all so much for your time. Stefanos.