Java 解析一个 YAML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25796637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:09:48  来源:igfitidea点击:

Parse a YAML file

javayaml

提问by Kristoffer Isaksson

This is the first time I am working with YAML files, so the first think I looked at was to find any library that could help me to parse the file.

这是我第一次使用 YAML 文件,所以我首先想到的是找到任何可以帮助我解析文件的库。

I have found two libraries, YamlBean and SnakeYAML. I am not sure which one that I am going to use.

我找到了两个库,YamlBean 和 SnakeYAML。我不确定我要使用哪一个。

Here is an example of the file that I am trying to parse:

这是我尝试解析的文件的示例:

users:
  user1:
    groups:
    - Premium
  user2:
    groups:
    - Mod
  user3:
    groups:
    - default
groups:
  Mod:
    permissions:
      test: true
    inheritance:
    - Premium
  default:
    permissions:
      test.test: true
    inheritance:
    - Mod
  Admin:
    permissions:
      test.test.test: true
    inheritance:
    - Mod

The file will change dynamical so I don't know how many users or groups the file would contain.

该文件将动态更改,因此我不知道该文件将包含多少用户或组。

The information I would like to fetch from this is the user name and the group like this:

我想从中获取的信息是用户名和组,如下所示:

user1 Premium
user2 Mod
user3 default

And from the groups only the group names, like this:

从组中只有组名,像这样:

Mod
default
Admin

Anyone could get me started here? And what is the best library to use for this? YamlBean or SnakeYAML?

任何人都可以让我从这里开始吗?用于此目的的最佳库是什么?YamlBean 还是 SnakeYAML?

I guess, I need to save the information in something that I easily could iterate over.

我想,我需要将信息保存在我可以轻松迭代的地方。

采纳答案by Kristoffer Isaksson

I ended up using SnakeYaml and made some split strings to solve my issue.

我最终使用了 SnakeYaml 并制作了一些拆分字符串来解决我的问题。

Loaded the yaml file to Objectand then into a Map, then split the result from the Mapinto String[]and then in a forloop I read out the name from the String[]. I did the same with groups.

将 yaml 文件加载到Object,然后加载到 a 中Map,然后将结果从Mapinto 中拆分出来String[],然后在for循环中我从String[]. 我对团体也做了同样的事情。

I know that there is better solutions out there but this is good enough for this project.

我知道有更好的解决方案,但这对于这个项目来说已经足够了。

Thanks all for the replies.

谢谢大家的回复。

回答by Tom

You could also use Hymansons YAML module.

您也可以使用Hymansons YAML 模块

In order to use that, you'll need a few classes. The model classes which will carry the content of your file and the a class that takes care of reading the YAML file.

为了使用它,你需要一些类。将携带文件内容的模型类和负责读取 YAML 文件的类。

The root model class could look like this:

根模型类可能如下所示:

public class MyYamlFile {
    @JsonProperty
    private List<User> users;
    @JsonProperty
    private List<Group> groups;

    // getter methods ommitted
}

The User(*) class:

User(*) 类:

public class User {
    @JsonProperty
    private List<String> name;
    @JsonProperty
    private List<GroupType> groups;

    // getter methods ommitted
}

The GroupType could be an Enum containing all possible group types:

GroupType 可以是包含所有可能的组类型的 Enum:

public enum GroupType {
    Premium, Mod, Default
}

Don't forget that the enum entries are case sensitive. So "premium" won't work. You can build all your model classes that way. Every sub entry should get an own model class.

不要忘记枚举条目区分大小写。所以“溢价”是行不通的。您可以通过这种方式构建所有模型类。每个子条目都应该有一个自己的模型类。

Now to the part where you can read that YAML file:

现在到您可以读取该 YAML 文件的部分:

public MyYamlFile readYaml(final File file) {
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // Hymanson databind
    return mapper.readValue(file, MyYamlFile.class);
}

As you can see, this part is really neat, because you don't need much. The file instance contains your YAML file. You can create one like this:

如您所见,这部分非常整洁,因为您不需要太多。文件实例包含您的 YAML 文件。你可以像这样创建一个:

File file = new File("path/to/my/yaml/usersAndGroups.yaml");

Instead of Filethe readValuemethod also supports InputStream, java.io.Reader, String(with the whole content), java.net.URLand byte array. You should find something that suits you.

代替FilereadValue方法还支持InputStream, java.io.Reader, String(带有全部内容)java.net.URL和字节数组。你应该找到适合你的东西。

(*) You should consider changing the structure of your YAML file, because I don't think it is possible to use dynamic keys with Hymanson (maybe someone knows more about that):

(*) 您应该考虑更改 YAML 文件的结构,因为我认为在 Hymanson 中使用动态密钥是不可能的(也许有人对此了解更多):

users: 
    - name: user1
      groups:
        - Premium
    - name: user2
      groups:
        - Mod
    - name: user3
      groups:
        - Default
groups:
    ....

回答by Tina21

YamlBean is included to DMelt Java numeric computational environment (http://jwork.org/dmelt/). You can create Yaml files using jhplot.io.HFileYAML class which creates a key-value map and save as yaml file.

YamlBean 包含在 DMelt Java 数值计算环境 ( http://jwork.org/dmelt/) 中。您可以使用 jhplot.io.HFileYAML 类创建 Yaml 文件,该类创建键值映射并另存为 yaml 文件。