Pacific-Design.com

    
Home Index

1. Java

2. YAML

Java / YAML /

/*
 * YamlBeans makes it easy to serialize and deserialize Java 
 * object graphs to and from YAML, a human-friendly data format.
 * Refrence: http://code.google.com/p/yamlbeans/
 */
package yaml;

import com.esotericsoftware.yamlbeans.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;

public class YAML {
    
    private static String yamlfile = "contact.yml";
    
    public static void main(String[] args) throws FileNotFoundException, YamlException {
        
        System.out.println("-------------------------------------------");
        get_first_record();
        System.out.println("-------------------------------------------");
        get_all_records();
    }
    /*------------------------------------------------------------------------*/

    private static void get_all_records() throws YamlException, FileNotFoundException {
        YamlReader reader = new YamlReader(new FileReader(yamlfile));
        while (true) {
            Map contact = (Map) reader.read();
            if (contact == null) break;            
            System.out.println(contact.get("name") + " " + contact.get("age"));
        }
    }
    /*------------------------------------------------------------------------*/
    
    private static void get_first_record() throws YamlException, FileNotFoundException {
        YamlReader reader = new YamlReader(new FileReader(yamlfile));
        Object object = reader.read();
        System.out.println(object);
        Map map = (Map) object;
        System.out.println(map.get("address"));
    }
    /*------------------------------------------------------------------------*/
}