Load .yml file into hashmaps using snakeyaml (import junit library)

喜欢而已 提交于 2019-12-04 07:43:56

I am finally trying the above code:

public static void main(String[] args) {
    // The path of your YAML file.
    final String fileName = "test.yml";
    ArrayList<String> key = new ArrayList<String>();
    ArrayList<String> value = new ArrayList<String>();
    Yaml yaml = new Yaml();

    try {
        InputStream ios = new FileInputStream(new File(fileName));

        // Parse the YAML file and return the output as a series of Maps and Lists
        Map< String, Object> result = (Map< String, Object>) yaml.load(ios);
        for (Object name : result.keySet()) {   

            key.add(name.toString());
            value.add(result.get(name).toString());    
        }

    } catch (Exception e) {
        e.printStackTrace();
    } 

     System.out.println(key + " " + value); }

Which actually reads .yml file and returns the keys and the correspondant values of the file. My problem is that value contains other objects which with their turn contains others Maps due to the order of the yaml file. Println shows the above in terminal:

[mean, eigenvalues, eigenvectors, projections, labels] [opencvmatrix@16e70aa, opencvmatrix@1a6e106, opencvmatrix@ff8927, [opencvmatrix@859445, opencvmatrix@1c94782, opencvmatrix@175e958, opencvmatrix@2acdad, opencvmatrix@254927, opencvmatrix@1e5c67f, opencvmatrix@4ace42, opencvmatrix@12b710e, opencvmatrix@10b6d81, opencvmatrix@142691b, opencvmatrix@102985e, opencvmatrix@16168c4, opencvmatrix@25417f, opencvmatrix@1ef0642, opencvmatrix@183d912, opencvmatrix@1204e58, opencvmatrix@1f78fae, opencvmatrix@1f9fe08, opencvmatrix@194ddfb], opencvmatrix@7996db]

How can I have access to the returned nested objects???

Edit2: Actually I had to cast the returned objects from the .yaml file to a new class that I have to define. So my code for reading .yml elements (face recognizer trained elements):

ArrayList<String> key = new ArrayList<String>();
    ArrayList<String> value = new ArrayList<String>();

    ArrayList<Object> obj = new ArrayList<Object>();
    ArrayList<Object> objected = new ArrayList<Object>();
    opencvmatrix mean = new opencvmatrix();
    opencvmatrix eigenValues = new opencvmatrix();
    opencvmatrix eigenVectors = new opencvmatrix();
    ArrayList<Object> opencvmatrix4 = new ArrayList<Object>();
    opencvmatrix labels = new opencvmatrix();
     opencvmatrix opencvmatrix6 = new opencvmatrix();
    ArrayList<ArrayList<Double>> projections = new ArrayList<ArrayList<Double>>();
    Yaml yaml = new Yaml();

    try {
        InputStream ios = new FileInputStream(new File(fileName));
        // Parse the YAML file and return the output as a series of Maps and Lists
        Map< String, Object> result = (Map< String, Object>) yaml.load(ios);
        for (Object name : result.keySet()) {
            key.add(name.toString());
            obj.add(result.get(name));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    File file = new File("eigenvalues.txt");
    // creates the file
    file.createNewFile();
    // creates a FileWriter Object
    FileWriter writer = new FileWriter(file);
        // Writes the content to the file


    mean = (opencvmatrix) (obj.get(0));
    eigenValues = (opencvmatrix) (obj.get(1));
    eigenVectors = (opencvmatrix) (obj.get(2));

    opencvmatrix4 = (ArrayList<Object>) obj.get(3);

    for(int i=0; i<opencvmatrix4.size(); i++){

        opencvmatrix6 = (opencvmatrix)opencvmatrix4.get(i);
        projections.add(opencvmatrix6.data);
    }

    //System.out.println(projections.get(0).size());

    labels = (opencvmatrix) (obj.get(4));
    //System.out.println(obj3.data.size());

    writer.write(eigenVectors.data.get(0) + "      ");
    temp.add(eigenVectors.data.get(0));
    eigenMatrix.add(temp);

    for (int i = 1; i < eigenVectors.data.size(); i++) {

        if (i % 3600 == 0) {
            writer.write("\n");
            eigenMatrix.add(temp);
            temp.clear();

        }

         writer.write(eigenVectors.data.get(i) + "      ");
        temp.add(eigenVectors.data.get(i));

    }
    writer.flush();
    writer.close();

SnakeYaml is YAML 1.1 parser and you are trying to parse YAML 1.0. It's indicated by "%YAML:1.0" directive (see 3.2.3.3 in the specs http://yaml.org/spec/1.0/#id2558635 )

YAML 1.1 has different syntax - "%YAML 1.1" (see 7.1.1 in the specs http://yaml.org/spec/1.1/#id895631 )

You can try to remove that directive from your file or change it to "%YAML 1.1" and hope it will be parsed correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!