以前は何も知らない yaml-cpp (0.5.1) ノードを歩こうとしています。エミッターとノードイベントを使用してそれを行う YAML::Dump があることは知っていますが、エミッターを使用しない方法があるかどうか疑問に思っています。
次のコードを試しましたが、必要なすべてのケースを特定するために必要なメソッドが見つかりませんでした:
void yaml_cpp_TestFunc_recursedown(YAML::Node& node)
{
for(YAML::const_iterator it=node.begin();it!=node.end();++it)
{
auto dit = *it;
try
{
if (dit.IsDefined())
{
try
{
std::cout << dit.as<std::string>() << std::endl;
}
catch (...) { }
yaml_cpp_TestFunc_recursedown(dit);
}
else {}
}
catch (const YAML::InvalidNode& ex)
{
try
{
if (dit.second.IsDefined())
{
try
{
std::cout << dit.second.as<std::string>() << std::endl;
}
catch (...) { }
yaml_cpp_TestFunc_recursedown(dit);
}
else {}
}
catch (...) { }
}
catch (...) { }
}
}
void yaml_cpp_Test()
{
const std::string testfile=".\\appconf.yaml";
{
// generate a test yaml
YAML::Node node;
node["paths"].push_back("C:\\test\\");
node["paths"].push_back("..\\");
node["a"] = "123";
node["b"]["c"] = "4567";
YAML::Emitter emitter;
emitter << node;
std::ofstream fout(testfile);
fout << emitter.c_str();
}
{
// try to walk the test yaml
YAML::Node config = YAML::LoadFile(testfile);
yaml_cpp_TestFunc_recursedown(config);
}
}