08-11
20
Caused by: org.xml.sax.SAXParseException: Premature end of file
作者:Java伴侣 日期:2008-11-20
昨天做JDOM解析XML的时候,一直报这个错误,CPU频频出现100%现象。今天从断点追踪了下,吓了自己一跳。
错误方法:
仔细一看,发现写在了迭代中,总是在输出,每读取一个节点,输出一次,还不关。
正确代码:
// 得到最大ID
public int getMaxId(String dir) {
int label = 0;
FileOutputStream fo = null;
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(dir + Constants.ROBOT_RULE);
Element root = doc.getRootElement();
List list = root.getChildren("rule");
Iterator j = this.getRules(dir).iterator();
while (j.hasNext()) {
for (int i = 0; i < list.size(); i++) {
Element e = (Element) list.get(i);
String id = e.getChild("id").getText();
if (id != null && Integer.parseInt(id) > label)
label = Integer.parseInt(id);
}
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
fo = new FileOutputStream(dir + Constants.ROBOT_RULE);
outp.output(doc, fo); } catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fo != null)
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return label;
}
错误方法:
复制内容到剪贴板 程序代码
// 得到最大ID
public int getMaxId(String dir) {
int label = 0;
FileOutputStream fo = null;
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(dir + Constants.ROBOT_RULE);
Element root = doc.getRootElement();
List list = root.getChildren("rule");
Iterator j = this.getRules(dir).iterator();
while (j.hasNext()) {
for (int i = 0; i < list.size(); i++) {
Element e = (Element) list.get(i);
String id = e.getChild("id").getText();
if (id != null && Integer.parseInt(id) > label)
label = Integer.parseInt(id);
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
fo = new FileOutputStream(dir + Constants.ROBOT_RULE);
outp.output(doc, fo);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fo != null)
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return label;
}
public int getMaxId(String dir) {
int label = 0;
FileOutputStream fo = null;
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(dir + Constants.ROBOT_RULE);
Element root = doc.getRootElement();
List list = root.getChildren("rule");
Iterator j = this.getRules(dir).iterator();
while (j.hasNext()) {
for (int i = 0; i < list.size(); i++) {
Element e = (Element) list.get(i);
String id = e.getChild("id").getText();
if (id != null && Integer.parseInt(id) > label)
label = Integer.parseInt(id);
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
fo = new FileOutputStream(dir + Constants.ROBOT_RULE);
outp.output(doc, fo);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fo != null)
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return label;
}
仔细一看,发现写在了迭代中,总是在输出,每读取一个节点,输出一次,还不关。
正确代码:
复制内容到剪贴板 程序代码
// 得到最大ID
public int getMaxId(String dir) {
int label = 0;
FileOutputStream fo = null;
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(dir + Constants.ROBOT_RULE);
Element root = doc.getRootElement();
List list = root.getChildren("rule");
Iterator j = this.getRules(dir).iterator();
while (j.hasNext()) {
for (int i = 0; i < list.size(); i++) {
Element e = (Element) list.get(i);
String id = e.getChild("id").getText();
if (id != null && Integer.parseInt(id) > label)
label = Integer.parseInt(id);
}
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
XMLOutputter outp = new XMLOutputter(format);
fo = new FileOutputStream(dir + Constants.ROBOT_RULE);
outp.output(doc, fo); } catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fo != null)
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return label;
}
评论: 0 | 引用: 0 | 查看次数: 5483
发表评论