简体   繁体   中英

Updating values in a yaml file using snakeYaml

Is there any way to edit/delete the values in a YAMLfile using java.

I'm using struts2-jquery-grid where the data will be populated from YAML file. Now, If I edit the field(s), it should be saved in YAML file. I am able to read the value from and write the values to a file in YAML format using yaml.load() and yaml.dump() respectively. But, I don't have any idea to do edit and delete the specific field.

I've gone through http://code.google.com/p/snakeyaml/wiki/Documentation#Dumping_YAML link where I read that we can use template processor and other options like defining order of java bean property, comments to make ease of yaml . But all those doesn't make any sense to me.

Here is my code to serialize the list of object called assumption here. In gridModel.add(assumption); , I'm storing the values of class Assumption(bean class with fetters and setters) to the list here in gridModel .

       gridModel.add(assumption);

       try {

        FileWriter pr=new FileWriter("D:/yaml.yaml");

        DumperOptions options = new DumperOptions();

         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

        Yaml yaml = new Yaml(options);
        String output=yaml.dump(gridModel);//,Tag.MAP, null);
        pr.write(output);
        System.out.print(pr.toString());
        System.out.println();

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

The outout I'm getting in a file called yaml.yaml as:

  • !!com.example.Assumption column1: '1' column2: Balwant column3: SPJ column4: rerer column5: null
  • !!com.example.Assumption column1: '' column2: Vikas column3: RNC column4: erer column5: null
  • !!com.example.Assumption column1: '3' column2: '' column3: PNBE column4: erer column5: null
  • !!com.example.Assumption column1: '4' column2: Diwakar column3: BGP column4: rerer column5: null
  • !!com.example.Assumption column1: '5' column2: Ajay column3: Godda column4: '' column5: null

For De-serialization the code is:

            InputStream input = new FileInputStream(new File("D:/yaml.yaml"));
    Yaml yaml = new Yaml();
    for (Object data : yaml.loadAll(input))
        System.out.println("Data:"+data);

The output to the console is :

Data:[Assumption [column1=1, column2=Balwant, column3=SPJ, column4=rerer, column5=null], Assumption [column1=, column2=Vikas, column3=RNC, column4=erer, column5=null], Assumption [column1=3, column2=, column3=PNBE, column4=erer, column5=null], Assumption [column1=4, column2=Diwakar, column3=BGP, column4=rerer, column5=null], Assumption [column1=5, column2=Ajay, column3=Godda, column4=, column5=null]]

I'm new to yaml and struts2. So, please answer a clean solution. Correct me, if I'm wrong while describing the requirement. Thanks in advance...

The only way I think to edit is to overwrite the existing yaml. We need to get the values from grid and overwrite ti to the existing one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM