2

i need to access the yaml file data into the java file. i used the YamlReader class and now the yaml file is loaded into the java class object. now all the information is in object and i want to extract it from this object.How can i do this . Can any one help me please i am stuck with this problem.

user1173437
  • 21
  • 1
  • 2

3 Answers3

0

Using snakeyaml, the code below didn't work for me. When the yaml object returned my object, it returned a LinkedHashMap type. So I cast my returned object into a LinkedHashMap.

public class YamlConfig {

    Yaml yaml = new Yaml();
    String path = "blah blah";
    InputStream input = new FileInputStream(new File(this.path));
    LinkedHashMap<String,String> map;

    @SuppressWarnings("unchecked")
    private void loadHashMap() throws IOException {
        Object data = yaml.load(input);// load the yaml document into a java object
         this.map = (LinkedHashMap<String,String>)data;
         System.out.println(map.entrySet()); //use this to look at your hashmap keys
         System.out.println(map);//see the entire map
    }
Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
0

You can read into a Map:

YamlReader reader = new YamlReader(new FileReader("contact.yml"));
Object object = reader.read();
System.out.println(object);
Map map = (Map)object;
System.out.println(map.get("address"));

Source: http://code.google.com/p/yamlbeans/

Hidde
  • 11,493
  • 8
  • 43
  • 68
0
Yaml yaml = new Yaml();
String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
List<String> list = (List<String>) yaml.load(document);
System.out.println(list);
Andrey
  • 2,931
  • 22
  • 18