读取xml (dom4j jdom两种方法)和 properties配置文件

所有类及配置文件都放在com.eagle.spring.factory包下
1。读取vehicle.properties
   vehicle.properties内容
vehicleType=com.eagle.spring.factory.Car

   测试类text:
package com.eagle.spring.factory;
import java.util.Properties;
public class Test {
public static void main(String args[]) throws Exception{
	Properties props = new Properties();// 获得property
	props.load(Test.class.getClassLoader().getResourceAsStream("com/eagle/spring/factory/vehicle.properties"));
//将配置文件作为inputStream导入
	String vehicleTypeName = props.getProperty("vehicleType");
	System.out.println(vehicleTypeName);
	Object o = Class.forName(vehicleTypeName).newInstance();
	Moveable m = (Car)o;
	m.run();
}
}

2、读取xml配置文件 (导入dom4j.jar和jaxen.jar包)

vehicle.xml内容
<?xml version="1.0" encoding="utf-8" ?> 
<config>
	<vehicle-type>
		<vehicle-name>com.eagle.spring.factory.Car</vehicle-name>
	</vehicle-type>
</config>

测试类test:
package com.eagle.spring.factory1;
import java.io.InputStream;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test {
	public static void main(String args[]) throws Exception{
		SAXReader reader = new SAXReader();//dom4j中SAXReader对象
		InputStream in = Thread.currentThread().getContextClassLoader().       getResourceAsStream("com/eagle/spring/factory/vehicle.xml");   
	    Document document=reader.read(in);   
		Element vehicleElt = (Element)document.selectObject("/config/vehicle-type/vehicle-name");            
		String vehicleName = vehicleElt.getStringValue();
		System.out.println(vehicleName);
		Moveable m = (Moveable)Class.forName(vehicleName).newInstance();
		m.run();
	    
	}
}



3、读取xml配置文件 (导入jdom.jar包)
test.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<HD>
  <disk name="C">
    <capacity>8G</capacity>
    <directories>200</directories>
    <files>1580</files>
  </disk>

  <disk name="D">
    <capacity>10G</capacity>
    <directories>500</directories>
    <files>3000</files>
  </disk>
</HD> 


测试类sample1代码
import java.util.*; 
import org.jdom.*; 
import org.jdom.input.SAXBuilder; 
public class Sample1 { 
  public static void main(String[] args) throws Exception{  
    SAXBuilder sb=new SAXBuilder(); 
    Document doc=sb.build(Sample1.class.getClassLoader().getResourceAsStream("test.xml")); //构造文档对象
    Element root=doc.getRootElement(); //获取根元素
    List list=root.getChildren("disk");//取名字为disk的所有元素 
    for(int i=0;i<list.size();i++){ 
       Element element=(Element)list.get(i); 
       String name=element.getAttributeValue("name"); 
       String capacity=element.getChildText("capacity");//取disk子元素capacity的内容 
       String directories=element.getChildText("directories"); 
       String files=element.getChildText("files"); 
       System.out.println("磁盘信息:"); 
       System.out.println("分区盘符:"+name); 
       System.out.println("分区容量:"+capacity); 
       System.out.println("目录数:"+directories); 
       System.out.println("文件数:"+files); 
       System.out.println("-----------------------------------"); 
    }   
  } 
} 

你可能感兴趣的:(java,spring,thread,xml)