package com.itheima.util;
//工具类可以抛,也可以处理.
public class Dom4jUtil {
//获取Document
public static Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/exam.xml");
return document;
}
//把数据写入XML中去
public static void write2xml(Document document) throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();//漂亮的格式,默认
format.setEncoding("UTF-8");//设置编码,默认UTF-8
XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format);
writer.write(document);
writer.close();
}
}
//工具类可以抛,也可以处理.
public class Dom4jUtil {
//获取Document
public static Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/exam.xml");
return document;
}
//把数据写入XML中去
public static void write2xml(Document document) throws Exception{
OutputFormat format = OutputFormat.createPrettyPrint();//漂亮的格式,默认
format.setEncoding("UTF-8");//设置编码,默认UTF-8
XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format);
writer.write(document);
writer.close();
}
}