获取WEB-INF的绝对路径和读改XML文件


package com.oaking.fund.cmb.util;
import org.w3c.dom.Document;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.*;
 import java.io.InputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import org.xml.sax.SAXException;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Node;
 import java.io.File;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;/**
 * <p>Title:密钥读改 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: HISUN</p>
 *
 * @author hwj
 * @version 1.0
 */
 public class KeyXMLTool {
     //保存密钥的XML文件
     private static String CMBKeyFileName="cmbKey.xml";
     //得到WEB-INF的绝对路径
     private static String web_inf_Path=new File(KeyXMLTool.class.getResource("/").getPath()).getParent()+"/WEB-INF";
     private static String CMBKeyFilePath=web_inf_Path+"/"+CMBKeyFileName;
     private static DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
     /**
      * 获取一Document对象
      * @param fileName String
      * @return Document
      */
     private static Document getDocumentByFileName(final String fileName){
         Document doc=null;
         try {
             DocumentBuilder dombuilder = domfac.newDocumentBuilder();
             InputStream in=new FileInputStream(fileName);
 //            InputStream in=ClassLoader.getSystemResourceAsStream(fileName);
 //            InputStream in = KeyXMLTool.class.getResourceAsStream("/Key.xml");
             doc=dombuilder.parse(in);
         } catch (ParserConfigurationException ex) {
             ex.printStackTrace();
         } catch (FileNotFoundException ex) {
             ex.printStackTrace();
         } catch (IOException ex) {
             ex.printStackTrace();
         } catch (SAXException ex) {
             ex.printStackTrace();
         }
         return doc;
     }
     public static String getPINKey(){
 //        System.out.println(">>>>>当前类所在绝对目录 "+KeyXMLTool.class.getResource(".").getPath());
 //        System.out.println(">>>>>当前类所在绝对目录 "+KeyXMLTool.class.getResource("").getPath());
 //        System.out.println("TEST PATH="+KeyXMLTool.class.getResource("/").getPath());
         String pinKey=null;
         Document doc=getDocumentByFileName(CMBKeyFilePath);
         NodeList pinKeyNodeList=doc.getElementsByTagName("pinkey");
         Node pinKeyNode=pinKeyNodeList.item(0);
         pinKey=pinKeyNode.getFirstChild().getNodeValue();
         return pinKey;
     }
     public static synchronized void setPINKEY(final String pinKey){
         Document doc=getDocumentByFileName(CMBKeyFilePath);
         NodeList pinKeyNodeList=doc.getElementsByTagName("pinkey");
         Node pinKeyNode=pinKeyNodeList.item(0);
         pinKeyNode.getFirstChild().setNodeValue(pinKey);
         doc2XmlFile(doc,CMBKeyFilePath);
     }
     
     public static String getMACKEY(){
         String macKey=null;
         Document doc=getDocumentByFileName(CMBKeyFilePath);
         NodeList pinKeyNodeList=doc.getElementsByTagName("mackey");
         Node pinKeyNode=pinKeyNodeList.item(0);     
         macKey=pinKeyNode.getFirstChild().getNodeValue();
         return macKey;    }
     
     public static synchronized void setMACKEY(final String macKey){        
         Document doc=getDocumentByFileName(CMBKeyFilePath);
         NodeList pinKeyNodeList=doc.getElementsByTagName("mackey");
         Node pinKeyNode=pinKeyNodeList.item(0);     
         pinKeyNode.getFirstChild().setNodeValue(macKey);
         doc2XmlFile(doc,CMBKeyFilePath);
     }
     
     /** 将document中的内容写入文件中   */
     public static boolean doc2XmlFile(Document document,String filename){ 
       boolean flag = true; 
       try { 
              TransformerFactory tFactory = TransformerFactory.newInstance();    
              Transformer transformer = tFactory.newTransformer(); 
             /** 编码 */ 
             //transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312"); 
              DOMSource source = new DOMSource(document); 
              StreamResult result = new StreamResult(new File(filename));    
              transformer.transform(source, result); 
          }catch(Exception ex) 
          { 
              flag = false; 
              ex.printStackTrace(); 
          } 
         return flag;       
     }     public static void main(String[] args){
 //        File tFile=new File(KeyXMLTool.class.getResource("/").getPath());
 //        System.out.println("父目录"+tFile.getParent());
 //        System.out.println(tFile.getAbsolutePath());
 //        System.out.println(">>>>>根目录 "+KeyXMLTool.class.getResource("/").getPath());
         System.out.println(">>>PINKEY="+getPINKey());
         setPINKEY("");
         System.out.println(">>>PINKEY22="+getPINKey());
         System.out.println(">>>MACKEY="+getMACKEY());
         setMACKEY("8#$$$$$$iio");
          System.out.println(">>>MACKEY22="+getMACKEY());
     }}

 

 

XML如下

<?xml version='1.0' encoding="UTF-8" ?>
 <key>
 <pinkey>121212</pinkey>
 <mackey></mackey>
 </key>