在C ++中解析YAML文件 [英] Parse YAML Files in C++

查看:1217
本文介绍了在C ++中解析YAML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的教程来显示我加载一个yaml文件并解析数据。 Expat样式将是伟大的,但任何解决方案,实际上以某种形式显示我的数据将是有用的。

I want a simple tutorial to show me to load a yaml file and parse the data. Expat style would be great but any solution that actually shows me the data in some form would be useful.

到目前为止我在yaml-0.1.1源运行多个测试C和我得到一个错误,没有输出什么,或在run-emitter.c情况。它读入yaml文件并将其打印到STDOUT,它不通过libyaml functions / structs生成文本。在有错误的情况下,我不知道是否是bc的文件是坏的或我的构建是不正确的(我没有修改任何东西...)该文件是从yaml.org复制的

So far I ran multiple test in yaml-0.1.1 source for C and I either get an error, no output what so ever or in run-emitter.c case. It reads in the yaml file and prints it to STDOUT, it does not produce the text via libyaml functions/structs. In the cases with an error I don't know if it was bc the file was bad or my build is incorrect (I didn't modify anything...) The file was copied from yaml.org

任何人都可以点我的教程? (我googled至少30分钟阅读任何看起来相关的内容)或一个lib的名称有一个很好的教程或示例。也许你可以告诉我哪个libyaml测试加载在文件中,并与它做某事或为什么我得到错误。本文档不会解释如何使用该文件,只会如何加载该文件。

Can anyone point me to a tutorial? (I googled for at least 30mins reading anything that looked relevant) or a name of a lib that has a good tutorial or example. Maybe you can tell me which libyaml test loads in files and does something with it or why I gotten errors. This document does not explain how to use the file, only how to load it.

http://pyyaml.org/wiki/LibYAML#Documentation

推荐答案

A Google代码搜索(现已停用)的yaml load lang:c ++将此作为第一个链接: demo.cc

A Google Code Search (now defunct) for "yaml load lang:c++" gave this as the first link: demo.cc:

#include <iyaml++.hh>
#include <tr1/memory>
#include <iostream>
#include <stdexcept>

using namespace std;

// What should libyaml++ do when a YAML entity is parsed?
// NOTE:  if any of the event handlers is not defined, a respective default
// no-op handler will be used.  For example, not defining on_eos() is
// equivalent to defining void on_eos() { }.
class my_handler : public yaml::event_handler {
    void on_string(const std::string& s) { cout << "parsed string:  " << s << endl; }
    void on_integer(const std::string& s) { cout << "parsed integer:  " << s << endl; }
    void on_sequence_begin() { cout << "parsed sequence-begin." << endl; }
    void on_mapping_begin() { cout << "parsed mapping-begin." << endl; }
    void on_sequence_end() { cout << "parsed sequence-end." << endl; }
    void on_mapping_end() { cout << "parsed mapping-end." << endl; }
    void on_document() { cout << "parsed document." << endl; }
    void on_pair() { cout << "parsed pair." << endl; }
    void on_eos() { cout << "parsed eos." << endl; }
};

// ok then, now that i know how to behave on each YAML entity encountered, just
// give me a stream to parse!
int main(int argc, char* argv[])
{
    tr1::shared_ptr<my_handler> handler(new my_handler());
    while( cin ) {
        try { yaml::load(cin, handler); } // throws on syntax error

        catch( const runtime_error& e ) {
            cerr << e.what() << endl;
        }
    }
    return 0;
}

这篇关于在C ++中解析YAML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆