Log log = LogFactory.getLog(XmlOperation.class);
// 要解析的xml文件路径
private String path;
private Document document;
private Element element;
public XmlOperation(String path) {
try {
if (!Util.checkfile(path)) {
log.error("要解析的xml文件不存在" + path);
return;
}
this.path = path;
SAXReader reader = new SAXReader();
// reader.setEncoding("UTF-8");
// new BufferedReader(new InputStreamReader(new
// FileInputStream(path)))
File file = new File(path);
document = reader.read(file);
element = document.getRootElement();
} catch (DocumentException e) {
log.error("读取xml文件出错" + e);
}
}
private void writeXML(String path, Document document) {
try {
OutputFormat format = OutputFormat.createPrettyPrint();
// format.setEncoding("UTF-8"); // 指定XML编码
XMLWriter writer = new XMLWriter(
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path), "UTF-8")), format);
writer.write(document);
writer.close();
} catch (IOException e) {
log.error("写xml文件出错" + e);
}
}
// 要解析的xml文件路径
private String path;
private Document document;
private Element element;
public XmlOperation(String path) {
try {
if (!Util.checkfile(path)) {
log.error("要解析的xml文件不存在" + path);
return;
}
this.path = path;
SAXReader reader = new SAXReader();
// reader.setEncoding("UTF-8");
// new BufferedReader(new InputStreamReader(new
// FileInputStream(path)))
File file = new File(path);
document = reader.read(file);
element = document.getRootElement();
} catch (DocumentException e) {
log.error("读取xml文件出错" + e);
}
}
private void writeXML(String path, Document document) {
try {
OutputFormat format = OutputFormat.createPrettyPrint();
// format.setEncoding("UTF-8"); // 指定XML编码
XMLWriter writer = new XMLWriter(
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path), "UTF-8")), format);
writer.write(document);
writer.close();
} catch (IOException e) {
log.error("写xml文件出错" + e);
}
}