xml简单工具类

来源:互联网 发布:网络电视会员怎么开通 编辑:程序博客网 时间:2024/04/28 10:22
/* * 运用Dom4j实现一个简单的工具类 */public class XMLUtils {   private XMLUtils(){}//获取文档对象public static Document getDocument(File file) {Document doc = null;SAXReader sax = new SAXReader();try {doc = sax.read(file);} catch (DocumentException e) {e.printStackTrace();}return doc;}//写入文档public static void writerToXml(Document doc,File file) {OutputFormat opf = OutputFormat.createPrettyPrint(); // 排版格式opf.setEncoding("utf-8");// OutputFormat opf=OutputFormat.createCompactFormat();//紧凑格式// 写入所需要的xml文件OutputStream os=null;XMLWriter xmlWriter=null;try{    os= new FileOutputStream(file);// 创建一个XMLWriter对象    xmlWriter = new XMLWriter(os, opf);//写出到目标文件xmlWriter.write(doc);} catch (IOException e) {e.printStackTrace();}finally{//手动关闭流try {xmlWriter.close();os.close();} catch (IOException e) {e.printStackTrace();}}}}

0 0
原创粉丝点击