Custom tool Classes---XML document reading tool classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.