dom4j讀取配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<add key="AD-DOMAIN" value="jspdi"></add>
<add key="AD-IP" value="172.17.16.1"></add>
<add key="com-domain" value="內網=http://172.17.17.105:8090;外網=http://218.94.40.4:8090"></add>
</configuration>

 

package com.pm360.pip.util.util;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadConfig
{
 
 @SuppressWarnings("unchecked")
 public static String  readConfig(HttpServletRequest request,String configName)
 {
  String configValue="";
  SAXReader reader=new SAXReader();
  Document dt;
  try {
   String uri=request.getRealPath("\\")+"WEB-INF\\projinfo-config.xml";
   dt = reader.read(new File(uri));
   List<Element>list=dt.selectNodes("/configuration/add");
   for (Element element : list)
   {
    String key=element.attribute("key").getValue();
    String value=element.attribute("value").getValue();
    if(key.equals(configName))
     configValue=value;
   }
  } catch (DocumentException e) {
   e.printStackTrace();
  }
 
  return configValue;
 }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章