用YAML取代java配置文件

玩过ruby 回头再看java的配置文件,是不是头大.yaml用来做配置文件简洁直观,一脚踢开xml吧.

Java YAML binding: Jyaml

Sample:

读取list
<!----> - hello
- boujour
- 你好
<!----> String[] al  =  Yaml.loadType( new  FileInputStream( new  File( " list.yml " )), String[]. class );
System.out.println(al.length);
System.out.println(al[
2 ]);


读取map
<!----> loadString: undefined
rootLocation: root
timeout: 15
<!----> Map ml  =  Yaml.loadType( new  FileInputStream( new  File( " map.yml " )), HashMap. class );
System.out.println(ml.size());
System.out.println(ml.get(
" timeout " ));

特定class
<!----> name:kaktos
age:5
gender:male
<!----> Person pl  =  Yaml.loadType( new  FileInputStream( new  File( " person.yml " )), Person. class );
System.out.println(pl.getName());
System.out.println(pl.getAge());
System.out.println(pl.getGender());


当然,jyaml还支持dump java class到yaml文件,映射更复杂的java类等.
是不是比xml简单好多呢~

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