dom4j 操作 xml

 

package com.doshine.platform.dras.security.impl;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;
import org.springframework.stereotype.Service;

import com.doshine.platform.dras.security.AdminDao;

@Service
public class AdminDaoImpl implements AdminDao{
 private static Log log = LogFactory.getLog(AdminDaoImpl.class);
 
 private static final String path = "C:\\dras_sh\\config\\config.xml";
 private static String adminGroups;
 private static String adminUsers;
 
 private synchronized static void initData() {
  // TODO Auto-generated method stub
  FileInputStream fin = null;
  SAXReader reader= null;
  
  try {
   fin = new FileInputStream(path);
   reader = new SAXReader();
   Document document = reader.read(fin);
   Element root = document.getRootElement();
   Element adminElement = root.element("admin");
   Element userElement = adminElement.element("users");
   if (userElement!=null)
   adminUsers  = userElement.getText();
   Element groupElement = adminElement.element("groups");
   if (groupElement!=null)
    adminGroups = groupElement.getText();
  } catch (Exception e) {
   // TODO: handle exception
   log.error(e);
  }finally{
   if (fin!=null)
    try {
     fin.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 }
 public void modifyAdminUsers(String adminUsers,String adminGroups)
 {
  FileInputStream fin = null;
  Document document =null;
  try {
   fin = new FileInputStream(path);
   SAXReader reader = new SAXReader();
   document = reader.read(fin);
   Element rootElement = document.getRootElement();
   Element adminElement = rootElement.element("admin");
   if (adminElement==null)
   {
    adminElement = DocumentHelper.createElement("admin");
    rootElement.add(adminElement);
   }
   Element usersElement = adminElement.element("users");
   if (usersElement==null)
   {
    usersElement = DocumentHelper.createElement("users");
    adminElement.add(usersElement);
   }
   usersElement.setText(adminUsers);
   Element groupsElement = adminElement.element("groups");
   if (groupsElement==null)
   {
    groupsElement = DocumentHelper.createElement("groups");
    adminElement.add(groupsElement);
   }
   groupsElement.setText(adminGroups);
   
   
  } catch (Exception e) {
   // TODO: handle exception
  }finally{
   if (fin!=null)
    try {
     fin.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     //e.printStackTrace();
     log.error(e);
    }
  }
  
  saveDocument(document);
  reflesh();
 }
 public static void reflesh()
 {
  initData();
 }
 
 private void saveDocument(Document document) {
  // TODO Auto-generated method stub
  FileOutputStream fos = null;
  BufferedWriter bw = null;
  XMLWriter writer=null;
  try {
   fos = new FileOutputStream(path);
   bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8"));
   writer = new XMLWriter(bw);
   writer.write(document);
   
  } catch (Exception e) {
   // TODO: handle exception
   log.error(e);
  }finally{
   if (writer!=null)
    try {
     writer.close();
    } catch (IOException e2) {
     // TODO Auto-generated catch block
     e2.printStackTrace();
    }
   if (bw!=null)
    try {
     bw.close();
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   if (fos!=null)
    try {
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 }
 public static void setAdminGroups(String adminGroups) {
  AdminDaoImpl.adminGroups = adminGroups;
 }
 public static String getAdminGroups() {
  if (adminGroups==null)
   initData();
  return adminGroups;
 }
 
 public static void setAdminUsers(String adminUsers) {
  AdminDaoImpl.adminUsers = adminUsers;
 }
 public static String getAdminUsers() {
  if (adminUsers==null)
   initData();
  return adminUsers;
 }
 

 public static void main(String[] args) {
  //System.out.println("ffffffffffffffff"+AdminDaoImpl.getAdminUsers());
  new AdminDaoImpl().modifyAdminUsers("adminUsers", "adminGroups");
  
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值