java xml作为配置文件,读取
来源:互联网 发布:杭州淘宝美工招聘网 编辑:程序博客网 时间:2024/05/19 22:26
每个项目都需要配置文件来把可变的东西抽出来,方便程序的开发和维护:
自己编写了一个xml文件读取类,并可以以字符串形式调用xml文件中的属性与其值,其实也可以,直接一个static函数直接获得属性与其值,然而,我没这么做。估计这么做会更好。不多说,就算是改也容易。先把source提供出来:
/* * Property.java * * Created on 2008.7.11, am8:56 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package mail;import java.io.File;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList;/** * * @author user */public class Property { private Element _rootElement = null; /** Creates a new instance of Property */ public Property() { //get the current directory File directory = new File(""); String pathStr = directory.getAbsolutePath(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File(pathStr + "//" + CONSTANT.xmlFileName)); _rootElement = document.getDocumentElement(); } catch (Exception e) { Logger.println("exception:" + e.getMessage()); } } /* get property with the specified name * param: property name * return: the value of the property */ public String getProperty(String propertyName){ try{ NodeList list = _rootElement.getElementsByTagName(propertyName); if(list.getLength() == 0){throw new Exception("xml error: there is no such property /"" + propertyName + "/"");} Element element = (Element) list.item(0); return element.getChildNodes().item(0).getNodeValue(); }catch(Exception ex){ Logger.println(ex.getMessage()); return ""; } } /* factory of this class * return the object of this class */ public static Property getInstance(){ Property _property = new Property(); return _property; } }
- java xml作为配置文件,读取
- java读取xml配置文件
- java读取xml配置文件
- JAVA读取XML配置文件
- java读取xml配置文件
- java读取XML配置文件
- java读取xml配置文件
- java读取xml配置文件(sax)
- java读取xml配置文件(sax)
- java读取xml配置文件(小结)
- java读取xml配置文件(小结)
- java读取xml/.properties配置文件
- 使用xml作为数据库的配置文件的路径读取问题
- JAVA技术:XML配置文件的读取处理
- JAVA技术:XML配置文件的读取处理
- JAVA解析XML配置文件的读取操作
- xml相关概念及java读取配置文件
- 利用java反射读取xml配置文件
- 天才思维解密
- 小猫烧香 - 核心源码
- linux驱动学习笔记(1)-2008-7-23
- 熊猫烧香
- 成就3D图形霸业!NVIDIA历代显卡回顾 网址
- java xml作为配置文件,读取
- jQuery插件[转]
- 宽容不会失去什么相反却会以此得人心
- PHP
- vim-ruby-2007.05.07 bin/install.rb学习 (二)
- 关于word中的taskpane管理解决方案
- iis安装经常出现的找不到xxx文件的一个原因及解决办法
- 正则表达式基础知识
- 开始→运行→命令集锦zz