我想抓住这个例外,我想在catch块中做一些处理代码:
try {
root.setText(page.getText(field.getLine() + repetition,
field.getColumn(),
field.getLength()));
} catch (org.jdom2.IllegalDataException e) {
// ignore: some lines can be empty and cannot
// be a valid XML character so we skip them
String invalidStr = page.getText(field.getLine() + repetition, field.getColumn(),field.getLength());
}
这是我使用e.printStackTrace()时打印的错误;方法:
org.jdom2.IllegalDataException: The data "
" is not legal for a JDOM character content: 0x000c is not a legal XML character.
at org.jdom2.Text.setText(Text.java:182)
那么如何测试字符0x000c的值:
String invalidStr = page.getText(field.getLine() + repetition, field.getColumn(),field.getLength());
if (invalidStr.equals(???)) {
}
提前谢谢
答案 0 :(得分:1)
if (invalidStr.indexOf(0x000C) >= 0) ...
或者
if (invalidStr.indexOf('\u000C') >= 0) ...
或者
if (invalidStr.contains("\u000C")) ...
(使用Unicode escape)
答案 1 :(得分:1)
对字符串对象使用“indexOf”方法:
String invalidStr = page.getText(field.getLine() + repetition, field.getColumn(),field.getLength());
if (invalidStr.indexOf((char)0x000C) >= 0) {
}