dom4j操作XML

来源:互联网 发布:nginx 修改请求路径 编辑:程序博客网 时间:2024/05/29 10:58
public class XmlUtils {
private static String filename = "users.xml";//模拟数据库
public static Document getDocument() throws DocumentException{
URL url = XmlUtils.class.getClassLoader().getResource(filename);
String realpath = url.getPath();
SAXReader reader = new SAXReader();
return reader.read(new File(realpath));//返回Document对象
}
public static void write2Xml(Document document) throws IOException{
URL url = XmlUtils.class.getClassLoader().getResource(filename);
String realpath = url.getPath();
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(realpath), format );
        writer.write( document );//向Document对象中写入数据
        writer.close();

}
}

0 0
原创粉丝点击