YAML的Java实现——JYAML基本原理与示例(1)导出数据为YAML格式文件

1. Overview

JYAML是YAML的Java实现,YAML的全称是YAML Ain't Markup Language,是否定递归定义,和LINUX的Linux Is Not UniX是一个意思。其结构之简单,常常成为导出或导入配置文件、数据结构等应用场景的常用API。


2. Download

http://jyaml.sourceforge.net/index.html


3. Basic Types

YAML包括Sequence和Map两种基本数据格式。对于Sequence,其每一项会以“-”开头。对于Map的每一项,key与value之间是用“:"来连接的。从属关系则用空格来表示。这基本上就是YAML的全部语法了。


4. Sample Code

(1)Data

package com.sinosuperman.yaml;

public class Person {
	private String name;
	private int age;
	private Person spouse;
	private Person[] children;
	public Person(){
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setSpouse(Person spouse) {
		this.spouse = spouse;
	}
	public void setChildren(Person[] children) {
		this.children = children;
	}
	public String getName() {
		return this.name;
	}
	public int getAge() {
		return this.age;
	}
	public Person getSpouse() {
		return this.spouse;
	}
	public Person[] getChildren() {
		return this.children;
	}
}

(2)Export Data(Dump Data to a YAML file)

package com.sinosuperman.yaml;

import java.io.File;
import java.io.FileNotFoundException;

import org.ho.yaml.Yaml;

public class TestYaml {
	
	public static void main(String[] args) {
		
		/* Initialize data. */
		Person michael = new Person();
		Person floveria = new Person();
		Person[] children = new Person[2];
		children[0] = new Person();
		children[1] = new Person();
		
		michael.setName("Michael Corleone");
		michael.setAge(24);
		floveria.setName("Floveria Edie");
		floveria.setAge(24);
		children[0].setName("boy");
		children[0].setAge(3);
		children[1].setName("girl");
		children[1].setAge(1);
		
		michael.setSpouse(floveria);
		floveria.setSpouse(michael);
		
		michael.setChildren(children);
		floveria.setChildren(children);
		
		/* Export data to a YAML file. */
		File dumpFile = new File("conf/testYaml.yaml");
		try {
			Yaml.dump(michael, dumpFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}

(3)YAML file

--- &0 !com.sinosuperman.yaml.Person
age: 24
children: &2 !com.sinosuperman.yaml.Person[]
  - !com.sinosuperman.yaml.Person
    age: 3
    name: boy
  - !com.sinosuperman.yaml.Person
    age: 1
    name: girl
name: Michael Corleone
spouse: !com.sinosuperman.yaml.Person
  age: 24
  children: *2
  name: Floveria Edie
  spouse: *0

5. 快去试试吧~

Java中,将字符串形式的多级YAML文件转换成实际的YAML文件通常涉及到两个步骤:解析字符串并构建YAML对象,然后将这个对象序列化回YAML格式。你可以使用开源库如SnakeYAML或者Jackson YAML来完成这样的任务。 首先,你需要添加相应的依赖到你的项目中。例如,如果你选择SnakeYAML,你可以通过Maven添加如下依赖: ```xml <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.36</version> </dependency> ``` 以下是基本的步骤: 1. **解析字符串到YAML对象**: 使用`org.yaml.snakeyaml.Yaml`类的`load()`方法,传入你的字符串输入,它会返回一个匹配的YAML对象(可能是`Map<String, Object>`或自定义的数据结构)。 ```java Yaml yaml = new Yaml(); Map<String, Object> yamlObject = yaml.load(yourStringInput); ``` 2. **构建多级YAML结构**: 根据YAML对象的层次结构来创建嵌套的Java对象,这可能需要递归或者手动处理键值对。 3. **生成新的YAML字符串**: 如果已经有一个Java对象,可以使用`Dumper`类将其转换回YAML字符串。 SnakeYAML有`DumperOptions`用于配置输出格式。 ```java DumperOptions options = new DumperOptions(); Yaml dumper = new Yaml(options); String formattedOutput = dumper.dump(yamlObject); ``` 4. **保存到文件**: 最后,你可以将`formattedOutput`写入到一个新的文件中,使用Java的I/O操作。 记得在实际应用中处理可能出现的错误,比如无效的YAML语法或者缺少必要的数据结构映射。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值