Java解析Yaml介绍

来源:互联网 发布:chart.js zoom y axis 编辑:程序博客网 时间:2024/06/07 21:11

序言:

YAML是一种类似XML和JSON的用于格式化数据的标记语言,它的可读性比起XML和JSON更高一些,在反编译Apk时就会生成这么一个YAML文件,里面写了一些配置信息,关于YAML的具体语法在这就不具体介绍了,网上百度一下还是很多的。下面就记录一下我在项目中怎么去解析YAML文件的,解析YAML文件,我使用了snakeyaml的jar包,jar包地址:下载。

使用:

1、首先是解析,代码如下:

        Yaml yaml = new Yaml();        //读入文件        Object result = yaml.load(yamlString);        System.out.println(result.getClass());        System.out.println(result);

这个Yaml有很多的load方法:
这里写图片描述
普通的load方法是将yaml文件加载为一个Map对象,你可以通过响应的键来取出响应的值,他还有一个loadAs方法能将yaml直接加载为一个对象。

2、Yaml对象 –> Yaml文档
yaml对象转化为Yaml文档,在Yaml类中也提供了响应的api:
这里写图片描述
代码:

            Yaml yaml = new Yaml();            String result = yaml.dumpAsMap(yamlMap);            System.out.println(result);

实例:

我使用的场景是对于反编译apk中yaml的解析:
代码:

public class YamlHelper {    public static final String RENAME_MANIFEST_PACKAGE_NODE = "renameManifestPackage";    public Map loadYaml(String path) {        try {            String yamlString = preLoad(path);            if (yamlString == null || yamlString.length() <= 0) {                return null;            }            //初始化Yaml解析器            Yaml yaml = new Yaml();            //读入文件            Object result = yaml.load(yamlString);            System.out.println(result.getClass());            System.out.println(result);            if (result instanceof Map) {                return (Map) result;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public void dumpYaml(Map yamlMap, String outPath) {        try {            //初始化Yaml解析器            Yaml yaml = new Yaml();            String result = yaml.dumpAsMap(yamlMap);            System.out.println(result);            File newFile = new File(outPath);            PrintWriter writer = new PrintWriter(newFile);            writer.print(result);            writer.flush();            writer.close();        } catch (Exception e) {            e.printStackTrace();        }    }    //这个方法主要作用是因为文件中有!!的一行,yaml无法进行解析,所以将其去掉    private String preLoad(String path) throws IOException {        File yamlFile = new File(path);        BufferedReader fileReader = new BufferedReader(new FileReader(yamlFile));        String temp;        StringBuilder stringBuilder = new StringBuilder();        while ((temp = fileReader.readLine()) != null) {            if (temp.trim().startsWith("!!")) {                continue;            }            stringBuilder.append(temp + "\n");        }        String result = stringBuilder.toString();        System.out.println("YAML 内容-->" + result);        return result;    }    public void removePackageNode(String path, String nodeTag) {        Map yamlMap = loadYaml(path);        System.out.println("Map-->: " + yamlMap);        Map pi = (Map) yamlMap.get("packageInfo");        if (pi.containsKey(nodeTag))            pi.remove(nodeTag);        dumpYaml(yamlMap, path);    }    /**     * 更改版本号、版本名     */    public void renameVersion(String path, VersionInfo versionInfo) {        Map yamlMap = loadYaml(path);        System.out.println("Map-->: " + yamlMap);        Map vi = (Map) yamlMap.get("versionInfo");        vi.put("versionCode", versionInfo.getVersionCode());        vi.put("versionName", versionInfo.getVersionName());        dumpYaml(yamlMap, path);    }    public VersionInfo getVersion(String path) {        Map yamlMap = loadYaml(path);        System.out.println("Map--> : " + yamlMap);        Map vi = (Map) yamlMap.get("versionInfo");        String code = (java.lang.String) vi.get("versionCode");        String name = (java.lang.String) vi.get("versionName");        System.out.println("versionCode: " + code);        System.out.println("versionName: " + name);        VersionInfo info = new VersionInfo();        info.setVersionCode(code);        info.setVersionName(name);        return info;    }    public static void main(String[] args) throws IOException {        String path = "./apktool.yml";        YamlHelper helper = new YamlHelper();        VersionInfo info = helper.getVersion(path);        info.setVersionCode(String.valueOf(Integer.valueOf(info.getVersionCode()) + 1));        helper.renameVersion(path, info);    }}

下载地址:

snakeyaml-1.17.jar点击下载

原创粉丝点击