读取*.xml和*.properties文件

需要的jar包
commons-configuration-1.6.jar

commons-lang-2.4.jar

commons-collections-3.2.1.jar

commons-digester-1.6.jar。(用2.0报空指针)

都需要!

1.读取xml文件
import java.util.List;   
import org.apache.commons.configuration.Configuration;   
import org.apache.commons.configuration.XMLConfiguration;   
public class XmlConfigurationExample {   
 public static void main(String[] args) throws Exception {   
  String resource = "com/discursive/jccook/configuration/global.xml";   
  Configuration config = new XMLConfiguration(resource);   
  //只有new一个XMLConfiguration的实例就可以了.   
  List startCriteria = config.getList("start-criteria.criteria");   
  int horsepower = config.getInt("horsepower");   
  System.out.println( "Start Criteria: " + startCriteria );   
  System.out.println(horsepower);   
 }   
}  



2.读取properties文件
import org.apache.commons.configuration.Configuration;   
import org.apache.commons.configuration.PropertiesConfiguration;   
public class PropertiesConfigurationExample {   
 public static void main(String[] args) throws Exception  {   
  Configuration config = new PropertiesConfiguration(    
"com/discursive/jccook/configuration/global.properties" );   
     
  System.out.println( "Speed: " + config.getFloat("speed"));   
  System.out.println( "Names: " + config.getString("name"));   
  }   
}  


你可能感兴趣的:(apache,xml)