This class is used to integrate read and write XML documents
Package Cn.jamsbwo.util;import Java.io.file;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.unsupportedencodingexception;import Org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.io.outputformat;import Org.dom4j.io.saxreader;import Org.dom4j.io.XMLWriter;/** * Tools for working with XML documents (read and write only) * read: Give the file path (or the file name under the project), get the Document Object * Write: Give the document to write and the path to write, return the write success * @author Administrator **/ Public classXmlutils {/** * Read document from specified path * @param filename File path * @return Read document * @throws Documentexception throws an exception /c0>*/ Public StaticDocument Read (String filename) throws documentexception{Try{Saxreader Reader=NewSaxreader (); Document Doc=reader.read (NewFile (filename)); returnDoc; } Catch(documentexception e) {e.printstacktrace (); Throwe; } } /** * Writes the specified document to the given path * @param doc needs to be written to the documents * @param filename written by the path * @return If the write succeeds returns True*/ Public Staticboolean writeto (Document doc,string filename) {Try{XMLWriter writer=NewXMLWriter (Newfileoutputstream (filename), outputformat.createprettyprint ()); Writer.write (DOC); return true; } Catch(Exception e) {System. out. println (E.getmessage ()); return false; } }}
Custom tool Classes---XML document reading tool classes