加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

YAML使用(2)——生成yaml文件

(2013-10-17 10:10:41)
标签:

yaml

生成文件

it

分类: 技术篇_其他技术

1. Overview

JYAML是YAML的Java实现,YAML的全称是YAML Ain't Markup Language,是否定递归定义,和LINUX的Linux Is Not UniX是一个意思。其结构之简单,常常成为导出或导入配置文件、数据结构等应用场景的常用API。


2. Download

http://jyaml.sourceforge.net/index.html


3. Basic Types

YAML包括Sequence和Map两种基本数据格式。对于Sequence,其每一项会以“-”开头。对于Map的每一项,key与value之间是用“:"来连接的。从属关系则用空格来表示。这基本上就是YAML的全部语法了。


4. Sample Code

(1)Data

 

  1. package com.sinosuperman.yaml;  
  2.   
  3. public class Person  
  4.     private String name;  
  5.     private int age;  
  6.     private Person spouse;  
  7.     private Person[] children;  
  8.     public Person(){  
  9.      
  10.     public void setName(String name)  
  11.         this.name name;  
  12.      
  13.     public void setAge(int age)  
  14.         this.age age;  
  15.      
  16.     public void setSpouse(Person spouse)  
  17.         this.spouse spouse;  
  18.      
  19.     public void setChildren(Person[] children)  
  20.         this.children children;  
  21.      
  22.     public String getName()  
  23.         return this.name;  
  24.      
  25.     public int getAge()  
  26.         return this.age;  
  27.      
  28.     public Person getSpouse()  
  29.         return this.spouse;  
  30.      
  31.     public Person[] getChildren()  
  32.         return this.children;  
  33.      
  34.  

(2)Export Data(Dump Data to a YAML file)

 

 

  1. package com.sinosuperman.yaml;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5.   
  6. import org.ho.yaml.Yaml;  
  7.   
  8. public class TestYaml  
  9.       
  10.     public static void main(String[] args)  
  11.           
  12.           
  13.         Person michael new Person();  
  14.         Person floveria new Person();  
  15.         Person[] children new Person[2];  
  16.         children[0new Person();  
  17.         children[1new Person();  
  18.           
  19.         michael.setName("Michael Corleone");  
  20.         michael.setAge(24);  
  21.         floveria.setName("Floveria Edie");  
  22.         floveria.setAge(24);  
  23.         children[0].setName("boy");  
  24.         children[0].setAge(3);  
  25.         children[1].setName("girl");  
  26.         children[1].setAge(1);  
  27.           
  28.         michael.setSpouse(floveria);  
  29.         floveria.setSpouse(michael);  
  30.           
  31.         michael.setChildren(children);  
  32.         floveria.setChildren(children);  
  33.           
  34.           
  35.         File dumpFile new File("conf/testYaml.yaml");  
  36.         try  
  37.             Yaml.dump(michael, dumpFile);  
  38.         catch (FileNotFoundException e)  
  39.             e.printStackTrace();  
  40.          
  41.      
  42.  

(3)YAML file

 

 

  1. --- &0 !com.sinosuperman.yaml.Person  
  2. age: 24  
  3. children: &2 !com.sinosuperman.yaml.Person[]  
  4.   !com.sinosuperman.yaml.Person  
  5.     age: 3  
  6.     name: boy  
  7.   !com.sinosuperman.yaml.Person  
  8.     age: 1  
  9.     name: girl  
  10. name: Michael Corleone  
  11. spouse: !com.sinosuperman.yaml.Person  
  12.   age: 24  
  13.   children: *2  
  14.   name: Floveria Edie  
  15.   spouse: *0  



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有