YAML的Java实现——JYAML基本原理与示例(2)导入YAML格式文件

请先阅读《YAML的Java实现——JYAML基本原理与示例(1)导出数据为YAML格式文件》一文。


1. 定义好的YAML文件testYaml.yaml

--- &0 !com.sinosuperman.yaml.Person
age: 24
children: &2 !com.sinosuperman.yaml.Person[]
  - !com.sinosuperman.yaml.Person
    age: 3
    name: boy
  - !com.sinosuperman.yaml.Person
    age: 1
    name: girl
name: Michael Corleone
spouse: !com.sinosuperman.yaml.Person
  age: 24
  children: *2
  name: Floveria Edie
  spouse: *0

或无类型信息的testYaml2.yaml

--- &0 
age: 24
children: &2 
  - 
    age: 3
    name: boy
  - 
    age: 1
    name: girl
name: Michael Corleone
spouse: 
  age: 24
  children: *2
  name: Floveria Edie
  spouse: *0

2. 导入YAML文件

try {
	Person corleone = (Person) Yaml.loadType(dumpFile, Person.class);
	Person corleone2 = (Person) Yaml.loadType(dumpFile2, Person.class);
	output(corleone);
	System.out.println();
	output(corleone2);
} catch (FileNotFoundException e) {
	e.printStackTrace();
}
...
private static void output(Person person) {
	System.out.println("Name: " + person.getName());
	System.out.println("Age: " + person.getAge());
	System.out.println("Spouse: " + person.getSpouse().getName());
	System.out.println("Children: " + person.getChildren()[0].getName() + 
		", " + person.getChildren()[1].getName());
}




原文链接: http://blog.csdn.net/poechant/article/details/7017088

你可能感兴趣的:(YAML的Java实现——JYAML基本原理与示例(2)导入YAML格式文件)