YAML:是YAML Ain't a Markup Language(YAML不是一種置標語言)的遞歸縮寫,早先YAML的意思實際上是:Yet Another Markup Language(另一種置標語言),但爲了強調這種語言以數據作爲中心,而不是以置標語言爲重點,而用返璞詞從新命名,YAML的官方定義很簡單,即「一種人性化的數據格式定義語言」,其主要功能用途相似於XML或JSON,YAML使用空白字符和分行來分隔數據,且巧妙避開各類封閉符號,如:引號、括號等,以免這些符號在複雜層次結構中變得難以辨認。YAML的語法與高階語言相似,能夠很簡單地表述序列(java中的list)、雜湊表(java中的map)、標量(java中的基本類型等)等數據結構,它重點強調可閱讀性。
YAML vs XML對比:
與YAML類似的數據格式定義語言是XML,YAML比XML優越性表如今
> - YAML的可讀性好
> - YAML和腳本語言的交互性好
> - YAML使用實現語言的數據類型
> - YAML有一個一致的信息模型
> - YAML易於實現
上面5條是XML不足的地方,同時,YAML也具備XML的下列優勢:
> - YAML能夠基於流來處理
> - YAML表達能力強,擴展性好
YAML相似於XML的數據描述語言,語法比XML簡單不少,YAML試圖用一種比XML更敏捷的方式,來完成XML所完成的任務。java
方式一、maven引入依賴包數據結構
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.16</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.jyaml</groupId> <artifactId>jyaml</artifactId> <version>1.3</version> </dependency>
方式2:直接引入jar包maven
在您的工程中引入如下jar便可:測試
(1)snakeyaml-1.16.jarthis
(2)Jackson-dataformat-yaml-2.7.3.jarspa
(3)Jackson-core-2.7.3.jarcode
(4)jyaml-1.3.jarorm
使用的model類:xml
package la.cewan.model; /** * Person類 * @author mengfeiyang * */ public class Person { private String name; private int age; private Person[] children; public Person(){ } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setChildren(Person[] children) { this.children = children; } public String getName() { return this.name; } public int getAge() { return this.age; } public Person[] getChildren() { return this.children; } }
package la.cewan.yaml; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import la.cewan.model.Person; import org.ho.yaml.Yaml; /** * yaml文件測試,將對象導出爲yaml文件 * @author mengfeiyang * */ public class TestYaml { public static void main(String[] args) { Person father = new Person(); father.setAge(12); father.setName("爺爺"); Person child1 = new Person(); child1.setAge(10); child1.setName("第一個孩子"); Person child2 = new Person(); child2.setAge(20); child2.setName("第二個孩子"); Person child3 = new Person(); child3.setAge(13); child3.setName("第一個孫子"); child1.setChildren(new Person[]{child3}); father.setChildren(new Person[]{child1,child2}); List<Person> pList = new ArrayList<Person>(); pList.add(father); pList.add(child1); pList.add(child2); pList.add(child3); HashMap<String,Person> pMap = new HashMap<String,Person>(); pMap.put("father", father); pMap.put("child1", child1); pMap.put("child2", child2); pMap.put("child3", child3); //String fPath = TestYaml.class.getResource("/testYaml.yaml").getFile(); File dumpFile = new File("testYaml.yaml"); try { Yaml.config.setSuppressWarnings(false); Yaml.dump(father, dumpFile); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
導出後的yaml文件內容:對象
--- !la.cewan.model.Person age: 12 children: !la.cewan.model.Person[] - !la.cewan.model.Person age: 10 children: !la.cewan.model.Person[] - !la.cewan.model.Person age: 13 name: 第一個孫子 name: 第一個孩子 - !la.cewan.model.Person age: 20 name: 第二個孩子 name: 爺爺
如下實例演示yaml文件中保存的對象映射到實際的java對象類。
package la.cewan.yaml; import java.io.File; import java.io.FileNotFoundException; import org.ho.yaml.Yaml; import la.cewan.model.Person; /** * 讀取yaml文件 * @author mengfeiyang * */ public class LoadYamlFile { public static void main(String[] args) { try { File dumpFile = new File("testYaml.yaml"); Person corleone = Yaml.loadType(dumpFile, Person.class); output(corleone); } catch (FileNotFoundException e) { e.printStackTrace(); } } private static void output(Person person) { System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); System.out.println("Children: " + person.getChildren()[0].getName() + ", " + person.getChildren()[1].getName()); System.out.println("GrandChild:"+person.getChildren()[0].getChildren()[0].getName()); } }
執行後的效果:
package la.cewan.yaml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import la.cewan.model.Person; import org.ho.yaml.YamlEncoder; /** * 導出yaml文件 * @author mengfeiyang * */ public class FileOutStream2Yaml { public static void main(String[] args) { try { File dumpFile = new File("testYaml.yaml"); YamlEncoder yEncoder = new YamlEncoder(new FileOutputStream(dumpFile)); for (int i = 0; i < 3; ++i) { Person michael = new Person(); michael.setAge(24 + i); michael.setName("michael"+i); yEncoder.writeObject(michael); yEncoder.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
導出的testYaml.yaml文件內容爲:
--- !la.cewan.model.Person age: 24 name: michael0 --- !la.cewan.model.Person age: 25 name: michael1 --- !la.cewan.model.Person age: 26 name: michael2
package la.cewan.yaml; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import org.ho.yaml.YamlDecoder; import la.cewan.model.Person; /** * 讀取yaml文件 * @author mengfeiyang * */ public class YamlFileInStream { public static void main(String[] args) { try { File dumpFile = new File("testYaml.yaml"); YamlDecoder yDecoder = new YamlDecoder(new FileInputStream(dumpFile)); Person[] persons = {new Person(), new Person(), new Person()}; for (int i = 0; i < 3; ++i) { persons[i] = (Person) yDecoder.readObject(); System.out.println(); output(persons[i]); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (EOFException e) { e.printStackTrace(); } } private static void output(Person person) { System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); } }
執行後的效果:
源碼地址:https://yunpan.cn/cBMGItygbcqvh
訪問密碼: c220