.xlsx ファイルからいくつかの文字列を取得しています (文字列は単純な文字です)。次に、それらの文字列を .xml ファイルに入れようとしています。しかし残念ながら、これらの文字列を「createElement(StringVariableHere)」メソッドに入れると、「org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.」というエラーが発生します。
この方法で文字列値を取得します。
switch (tempCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
String tempColValue = tempCell.getStringCellValue();
}
これは、文字列値を追加しようとする行であり、エラーが発生します。
Element titleChild = doc.createElement(StringVariableHere);
オンラインで見つけた次の方法を使用して、文字列をきれいにしようとしました。
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();
}
また、次を使用して有効かどうかを確認しています。文字列を追加すると、false が返されます。
XMLChar.isValidName(StringVariableHere)
お時間をいただきありがとうございました。ステファノス。