package cn.lnu.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XmlUtils { private static String filepath; static{//��̬����飬ִֻ��һ�� //����ļ�users.xml��λ�� filepath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath(); } //���xml�ĵ� public static Document getDocument() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File(filepath)); return document; } //���ڴ����޸�֮�������д�ص�xml�ĵ��� public static void writeToXml(Document document) throws IOException{ // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter( new FileOutputStream(filepath), format ); writer.write( document ); writer.close(); } }