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");
  
 }
}

你可能感兴趣的:(dom4j 操作 xml)