我正在使用snakeyaml库来解析yaml文件,并稍后从xml中转储它。我想知道是否有任何方法可以控制最终的 yaml 缩进。例如,最终文件中的列表将如下所示:
list:
- "first item"
- "second item"
我想在列表项之前添加一些空格。最终结果应该像这样:
list:
- "first item"
- "second item"
我发现可以添加自定义解析器和代表器。但也不让我添加额外的空格。我已经看到在 ScalarNode 类中,有一些标记包含有关起始列和结束列的信息,但这些标记仅用于记录目的。有谁知道这种情况的解决方案吗?
要应用所需的缩进,请应用下一个配置:
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setIndicatorIndent(2);
options.setIndentWithIndicator(true);
属性 IndicatorIndent 和 IndentWithIndicator 应用此格式输出。
DumperOptions.setIndicatorIndent() 将满足您的需要。
也许对其他人来说很明显,但对我来说却不然......
DumperOptions
必须传递给 Yaml
构造函数。例如:
DumperOptions options = new DumperOptions();
options.setIndent(2);
# More options...
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter("out.yml");
yaml.dump(myData, writer);