首页 > 其他分享 >获取WEB-INF的绝对路径和读改XML文件

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

时间:2023-03-27 22:01:47浏览次数:32  
标签:XML WEB String xml doc 绝对路径 static import Document


获取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>

标签:XML,WEB,String,xml,doc,绝对路径,static,import,Document
From: https://blog.51cto.com/u_16034393/6153302

相关文章

  • PHP5 soap web services
    一.开发环境:1WAMPserverPHP5.3+apache+mysql的集成环境2.Eclipse+PHP插件3。python2.5和PHP5语言版本二:开发代码1.python做一个服务端,发布为python......
  • 流量分析:陇剑杯webshell
    参考:https://blog.csdn.net/lkbzhj/article/details/126343675 题目描述:单位网站被黑客挂马,请您从流量中分析出webshell,进行回答: 1、黑客登录系统使用的密码是过......
  • 使用unittest运行第一个web自动化测试用例
    unittest:和pytest一样的测试框架,区别在于pytest是第三方框架,unittest是标准库框架一、框架的分层:1、新建一个项目文件夹,并在vscode中打开它          ......
  • 百度2020校招Web前端工程师笔试卷(第三批)(大题21~23)
    <!DOCTYPEhtml><html><head><metacharset="UTF-8"><style>body,ul,li,select{margin:0;padding:0;box-sizing:border-box......
  • web自动化中浏览器实例化优化及关联
    在一个测试案例py文件中,可能会有多个测试案例,比如登录,会有登录成功和登录失败的情况,执行测试用例后,就会打开两个网页。为了解决这个问题,需要进行优化来解决浏览器重复打开......
  • 小皮Windows web面板漏洞详解
    漏洞简介PhpStudy国内12年老牌公益软件,集安全、高效、功能与一体,已获得全球用户认可安装,运维也高效。支持一键LAMP、LNMP、集群、监控、网站、数据库、FTP、软件中心......
  • 利用dom4j修改XML数据_进行增删改查【最新】
    下面只发studentDao与util的源码:代码已经测试过了,没有问题,如果有需要的调整,会修改,如有错误请帮忙改正,下午整理了一下修改,其实跟查询差不多,只是设置一下元素体即可,代码已经更......
  • 优化 vue 使用 webpack 打包,出现的缓存问题 及 项目部署问题
    https://blog.csdn.net/qq_39264561/article/details/124844878`constpath=require("path");constwebpack=require("webpack");consttimeStamp=newDate().ge......
  • webpack4.0核心概念(十一)———— babel
    官⽅⽹站:https://babeljs.io/中⽂⽹站:https://www.babeljs.cn/Babel是JavaScript编译器,能将ES6代码转换成ES5代码,让我们开发过程中放⼼使⽤JS新特性⽽不⽤担⼼兼......
  • android stdio中java代码对于xml的操作
    首先,我们需要在java代码中声明要修改的页面,例如LinearLayoutlistView=findViewById(R.id.line_station_liebiao);其次声名我们加入希望添加的组件,例如LinearLayout......