• paip.读取WEB.XML中的参数值总结


    paip.读取WEB.XML中的参数值总结

    ----作者Attilax , 1466519819@qq.com---


    比如WEB.XML中自定义以下参数

        <context-param>
            <param-name>serverNO</param-name>
            <param-value>02</param-value>
        </context-param>


    则读取的时候可以使用SERVLET中的方法 ...

    但是,如果在WEB服务器不起动的情况下,或者在后台类中读取其配置值,就需要换个思路了..以读取XML文件的方式来获取配置值..

    主要思路如下:
    1.得到CLASS路径值。。
    2.得到其父路径,即WEB-INF的路径值。
    3.得到完整的WEB-INF\WEB.XML的路径值
    4.获取WEB.XML文件内容,并解析XML
    5.取得context-param的参数值..

    ----------源码如下:--------------------
    package m;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.List;

    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.XPath;

    import mole.Config;

    public class WebCfgRead {

     /**
      * @param args
      * @throws IOException
      * @throws DocumentException
      */
     public static void main(String[] args) throws DocumentException,
       IOException {

      // Attribute key= (Attribute) add.
      System.out.print("value:" + WebCfgRead.getValue("serverNO"));

     }

     static String getConfigXml() throws IOException {
      // if WEB-INF webapp else desktopApp
      // webApp path=
      //  String curpath = (System.getProperty("user.dir")
      //    + java.io.File.separator + "src" + File.separator);
      //  curpath += "mole.config.xml";
      String curpath = "/mole.config.xml";
      URL url = Thread.currentThread().getContextClassLoader()
        .getResource("");
      String url2 = m.path.parentDir(url.getPath());
      String springCfgPath = url2 + File.separator + "web.xml";
      // InputStream resourceAsStream = new Config().getClass().getResourceAsStream(curpath);

      //String fileContent = mole.io.FileService.read(curpath);
      String fileContent = mole.io.FileService.read(springCfgPath);
      //  resourceAsStream=null;
      //  System.gc();
      return fileContent;

     }

     public static String getValue(String keyName) throws DocumentException,
       IOException {
      String xmlStr = "  <config> "
        + "<item key=\"aa_key\" value=\"aa_value\" /> "
        + "<item key=\"bb_key\" value=\"bb_value\" />" + "</config>";
      xmlStr = getConfigXml();
      //  <config> <item key="aa_key" value="aa_value" /> <item key="bb_key" value="bb_value" /></config>
      org.dom4j.Document document = DocumentHelper.parseText(xmlStr);
      Node root = document.getRootElement();
      // root.selectNodes(arg0, arg1)
      String defaultNamespace = document.getRootElement().getNamespaceURI();
      HashMap nsMap = new HashMap();
      nsMap.put("ns", defaultNamespace);
      XPath xsub = document.createXPath("ns:context-param");
      xsub.setNamespaceURIs(nsMap);//加入命名空间

      List list = xsub.selectNodes(root);

      String returnValue = "";
      for (int i = 0; i < list.size(); i++) {
       Node item = (Node) list.get(i);
       XPath x2 = document.createXPath("ns:param-name");
       x2.setNamespaceURIs(nsMap);//加入命名空间
       Node paraName = x2.selectSingleNode(item);

       String key = paraName.getStringValue();

       XPath x3 = document.createXPath("ns:param-value");
       x3.setNamespaceURIs(nsMap);//加入命名空间
       String value = x3.selectSingleNode(item).getStringValue();
       if (key.equals(keyName)) {
        returnValue = value;
        break;
       }
      }
      return returnValue;

     }

    }


     

  • 相关阅读:
    hibernate中的所有属性
    新闻系统(3)内容保护的探索
    Hibernate常见问题集锦
    java对象序列化java.io.Serializable 接口实践
    读写属性文件
    Struts 中的自定义异常
    JAVA程序员看C#的精华与糟粕
    正确理解hibernate的inverse属性
    HTML5中的WebSocket
    【转载】 单元测试:是否针对非 public method 进行测试?
  • 原文地址:https://www.cnblogs.com/attilax/p/15199754.html
Copyright © 2020-2023  润新知