XML配置文件
来源:互联网 发布:手机淘宝咋退货退款 编辑:程序博客网 时间:2024/06/06 10:46
1.什么是配置文件?(FAQ)
用户配置文件就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。
2.配置文件都有什么不同的区别?
主要有三种主要的配置文件类型:
(1)·本地用户配置文件。
(2)·漫游用户配置文件。一个本地配置文件的副本被复制及储存在网络上的一个服务器共享上。
(3)·强制用户配置文件。是一种特殊类型的配置文件,使用它管理员可为用户指定特殊的设置。只有系统管理员才能对强制用户配置文件作修改。当用户从系统注销时,用户对桌面做出的修改就会丢失。
(4).临时的配置文件,只有在因一个错误而导致用户配置文件不能被加载时才会出现。临时配置文件允许用户登录并改正任何可能导致配置文件加载失败的配置。临时配置文件在每次会话结束后都将被删除或注销时对桌面设置和文件所作的更改都会丢失。
3.读取应用程序的配置文件
1.在.Net
(1)
读写配置文件app.config
在.Net中提供了配置文件,让我们可以很方面的处理配置信息,这个配置是XML格式的。而且.Net中已经提供了一些访问这个文件的功能。
(2)
设置配置信息
如果配置信息是静态的,我们可以手工配置,要注意格式。如果配置信息是动态的,就需要我们写程序来实现。在.Net中没有写配置文件的功能,我们可以使用操作XML文件的方式来操作配置文件。
2.在VS2005中
对于应用程序配置文件的读写一般使用Configuration,ConfigurationManager两个类。
3.在java中
(1)利用java反射读取xml配置文件
利用java反射机制创建类的实例分为两种情况:带参数和不带参数
a:
Class c = Class.forName("className");//返回与带有给定字符串名的类 或接口相关联的 Class 对象。
Object object = c.newInstance();//创建此 Class 对象所表示的类的一个新实例。
System.out.println(object);
b:
Class c=Class.forName("className");
Class[] ptype=new Class[]{double.class,int.class};
Constructor constructor=c.getConstructor(ptype);//返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法
Object[] obj=new Object[]{new Double(3.1415),new Integer(123)};
Object object=construct.newInstance(obt);//使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
System.out.println(object);
(2)使用dom4j解析xml
需要:dom4j-2.0.0-ALPHA1.jar
a:
<?xml version="1.0" encoding="UTF-8"?>
<package>
<action name="hello" class="com.flyoung.HelloWorldIml"></action>
</package>
b:
package com.flyoung;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.Iterator;
import java.util.List;
import java.io.File;
public class TestDom4j {
/**
* 获取指定xml文档的Document对象,xml文件必须在classpath中可以找到
*
* @param xmlFilePath xml文件路径
* @return Document对象
*/
public static Document parse2Document(String xmlFilePath){
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new File(xmlFilePath));
} catch (DocumentException e) {
e.printStackTrace();
}
return doc;
}
public static void testParseXmlData(String xmlFilePath){
//获取xml解析器对象
//SAXReader reader = new SAXReader();
//将xml解析为Document对象
Document doc = TestDom4j.parse2Document(xmlFilePath);
//获取文档的根元素
Element root = doc.getRootElement();
//定义保存xml数据的缓冲字符串
StringBuffer sb = new StringBuffer();
for(Iterator i_action=root.elementIterator();i_action.hasNext();){
Element e_action = (Element)i_action.next();
for(Iterator a_action=e_action.attributeIterator();a_action.hasNext();){
Attribute attribute = (Attribute)a_action.next();
sb.append(attribute.getName()+":"+attribute.getValue());
sb.append("\n");
}
}
System.out.println(sb);
}
public static void main(String[] args) {
TestDom4j.testParseXmlData("E:/workspace/Dom4j/test.xml");
}
}
(3)使用java发射机制创建类的实例
业务接口:
package com.flyoung;
public interface HelloWorld {
public void sayHelloWorld();
}
业务接口实现:
package com.flyoung;
public class HelloWorldIml implements HelloWorld {
public void sayHelloWorld() {
System.out.println("Hello World!!!");
}
}
读取xml配置文件创建类的实例
package com.flyoung;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.Iterator;
import java.util.List;
import java.io.File;
import java.util.Map;
import java.util.HashMap;
public class TestDom4j {
/**
* 获取指定xml文档的Document对象,xml文件必须在classpath中可以找到
*
* @param xmlFilePath xml文件路径
* @return Document对象
*/
public static Document parse2Document(String xmlFilePath){
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new File(xmlFilePath));
} catch (DocumentException e) {
e.printStackTrace();
}
return doc;
}
public static Map testParseXmlData(String xmlFilePath){
//获取xml解析器对象
//SAXReader reader = new SAXReader();
//将xml解析为Document对象
Document doc = TestDom4j.parse2Document(xmlFilePath);
//获取文档的根元素
Element root = doc.getRootElement();
//定义保存xml数据的缓冲字符串
//StringBuffer sb = new StringBuffer();
//定义保存属性、值的map
Map<String,String> map = new HashMap<String,String>();
for(Iterator i_action=root.elementIterator();i_action.hasNext();){
Element e_action = (Element)i_action.next();
for(Iterator a_action=e_action.attributeIterator();a_action.hasNext();){
Attribute attribute = (Attribute)a_action.next();
//sb.append(attribute.getName()+":"+attribute.getValue());
//sb.append("\n");
map.put(attribute.getName(), attribute.getValue());
}
}
//System.out.println(sb);
return map;
}
public static void main(String[] args) {
Map map = TestDom4j.testParseXmlData("E:/workspace/Dom4j/test.xml");
String className =(String)map.get("class");
try {
Class c = Class.forName(className);
HelloWorld hw =(HelloWorld) c.newInstance();
hw.sayHelloWorld();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
参考来源
http://www.cnblogs.com/flyoung2008/archive/2011/10/05/2199613.html
- XML配置文件
- XML配置文件
- xml配置文件
- 读取XML配置文件
- DWR.xml配置文件学习
- C#读取XML配置文件
- DWR.xml配置文件学习
- DWR.xml配置文件说明书
- PHP获取XML配置文件
- log4j XML配置文件
- applicationContext.xml的配置文件
- dwr.xml配置文件
- Webwork2 WEB.xml配置文件
- Tomcat 配置文件web.xml
- struts2 配置文件 struts.xml
- struts-config.xml配置文件
- hibernate.cfg.xml 配置文件
- java读取xml配置文件
- caffe make runtest 错误
- iOS水波纹动画详解
- LeetCode-141:Linked List Cycle
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- 关于JAVA中的listener回调机制,以及其在Android中的使用
- XML配置文件
- ERROR 1146 (42S02): Table 'voucherdb.fvr_vouchersummary' doesn't exist
- DB2数据库的学习及总结
- kth largest element in an array
- 反射生成对象 获得方法及构造器
- java中HashMap的用法
- 《TCP/IP卷》读书笔记
- poj 1990 MooFest(转化成树状数组求和)
- spark sql基本使用方法介绍