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.
did you try looking at the string you get in java? printing it to the console or such?
It reminds me of similar issues i had parsing office document. The parser (apache POI) was sometimes giving unicode invalid characters which were breaking the xml (one example was with line breaks).
I don’t know what parser you are using but you might have to clean your string before trying to fill your xml.
Edit after the added details.
What kind of xml are you trying to write? can you provide an example?
doc.createElement(StringVariableHere) means you try to create an element named StringVariableHere. I.E.
not