Results 1 to 2 of 2
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Reading a yaml file using snakeyaml

    I am trying to read a yaml file using snakeyaml. My code is the following:
    Java Code:
      public static void main(String args[]) throws IOException{
    
           final Yaml yaml = new Yaml();
            Reader reader = null;
            LinkedHashMap lhm = new LinkedHashMap();
            try {
                reader = new FileReader("example.yml");
              
                System.out.println("yaml: "+ yaml.load(reader));
            } catch (final FileNotFoundException fnfe) {
                System.err.println("We had a problem reading the YAML from the file because we couldn't find the file." + fnfe);
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (final IOException ioe) {
                        System.err.println("We got the following exception trying to clean up the reader: " + ioe);
                    }
                }
            }
            
        }
    The resylt of system.out is yaml: {accountCode=foo, accountId=1, email=foo@bar.com, userId=1}. How can I access the element of the yaml file seperately. I want finally to store accountCode, accountID, email, userID variables with their values???

  2. #2
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Re: Reading a yaml file using snakeyaml

    Basically when my file is like the following:
    Java Code:
    mean: opencvmatrix
    rows: 1
    cols: 3600
    dt: d
    data:  [9.6842105263157890e+01, 1.0257894736842104e+02,
           1.0557894736842104e+02, 1.0794736842105263e+02,
           1.1752631578947367e+02, 1.1631578947368421e+02,
           1.1084210526315789e+02, 1.0373684210526315e+02,
           1.0052631578947368e+02, 9.5263157894736835e+01,
           9.0421052631578945e+01, 8.5631578947368411e+01,
           7.8684210526315780e+01, 7.2105263157894740e+01,
           6.9315789473684205e+01, 6.9105263157894740e+01,
           7.1052631578947370e+01, 7.9631578947368411e+01,
           9.0894736842105260e+01, 1.0121052631578947e+02,
           1.1468421052631578e+02, 1.2194736842105263e+02,
           1.4584210526315789e+02, 1.5763157894736841e+02]
    I can read it properly, but when I put two semicolons in front of opencvmatrix, I get the error: Can't construct a java object for tag:yaml.org,2002:opencvmatrix; exception=No single argument constructor found for class opencvmatrix
    in 'reader', line 1, column 7:
    mean: !!opencvmatrix

    Actually the structure of my file:
    Java Code:
    mean: !!opencvmatrix
       rows: 1
       cols: 3600
       dt: d
       data:  [9.6842105263157890e+01, 1.0257894736842104e+02,
           1.0557894736842104e+02, 1.0794736842105263e+02,  
           1.1752631578947367e+02, 1.1631578947368421e+02,
           1.1084210526315789e+02, 1.0373684210526315e+02,
           1.0052631578947368e+02, 9.5263157894736835e+01,
           9.0421052631578945e+01, 8.5631578947368411e+01,
           7.8684210526315780e+01, 7.2105263157894740e+01,
           6.9315789473684205e+01, 6.9105263157894740e+01,
           7.1052631578947370e+01, 7.9631578947368411e+01,
           9.0894736842105260e+01, 1.0121052631578947e+02,
           1.1468421052631578e+02, 1.2194736842105263e+02,
           1.4584210526315789e+02, 1.5763157894736841e+02]
    projections:
       - !!opencvmatrix
          rows: 1
          cols: 19
          dt: d
          data: [ 1.6852352677811423e+03, -1.0112905042030820e+03,
              -1.3152188243875064e+03, 4.8298990155435700e+02,
              9.1980535900698968e+01, 6.5624763621018792e+02,
              -1.2380244625181117e+03, -1.8800931631152707e+02,
              -4.1473232893431384e+02, 5.7197035514178856e+02,
              4.9990276749703435e+02, 4.9062987890888604e+02,
              6.1928710580829818e+02, -1.2842392693864540e+03,
              2.4907719466932619e+01, 8.0299588479341844e+02,
              1.1621501008120421e+02, 1.0410492864645674e+02,
              8.5302163830384785e+01 ]
    Last edited by zenitis; 01-16-2014 at 03:58 PM.

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  2. Reading file external to jar file
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:46 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •