【问题标题】:How to get complete yaml node child object from a parent yaml node?如何从父 yaml 节点获取完整的 yaml 节点子对象?
【发布时间】:2014-03-19 15:15:27
【问题描述】:

我试图在解析 yaml 文件时获取完整的节点对象。 我的 yaml 数据类似于下面的数据

---
Parent:
  Child1: ABC
  Child2:
    Subchild1: 123
    Subchild2: 456
  Child3: XYZ
...

我试图获取数据的方式是

YAML::Node parentNode = YAML::LoadFile(abc.yaml); // My yaml file name is abc

    if(!parentNode.IsNull())
    {
        if(parentNode.IsMap())
        {
            YAML::iterator it = parentNode.begin();
            std::cout << "Parent is " << it->first.as<std::string>() << std:endl; // Parent
            if(it->second.IsScalar())
            {
            }
            else if(it->second.IsMap())
            {
                YAML::Node rootChild = it->second;
                YAML::iterator chilItr = rootChild.begin();
                std::cout << "Child count is " << chilItr->second.size() << std:endl; // 3
                while(chilItr != rootChild.end())
                {
                    YAML::Node child = *chilItr;
                    if(child.IsMap()) //This causes exceetion
                    {
                        YAML::iterator ChildIterator = Child.begin();
                        std::cout << " Child is " << ChildIterator->first.as<std::string>() << std::endl;
                    }
                    chilItr++;
                }
            }
        }
    }

为什么child.IsMap() 会抛出异常? 所以基本上要求是如何从父对象中获取子 YAML 节点对象?

【问题讨论】:

    标签: yaml-cpp


    【解决方案1】:

    我不认为您粘贴了您正在使用的确切 YAML 文件或确切代码,因为使用您的示例,您对 3 的评论不会是打印的内容。这是基本思想,使用您的示例 YAML 文件:

    YAML::Node parentNode = YAML::LoadFile("abc.yaml");
    if (parentNode.IsMap()) {
      YAML::iterator it = parentNode.begin();
      YAML::Node key = it->first;
      YAML::Node child = it->second;
      std::cout << "Parent is " << key.as<std::string>() << "\n"; // Parent
      if(child.IsMap()) {
        std::cout << "Child count is " << child.size() << "\n"; // 3
    
        // Now that you've got a map, you can iterate through it:
        for (auto it = child.begin(); it != child.end(); ++it) {
          YAML::Node childKey = it->first;
          YAML::Node childValue = it->second;
    
          // Now you can check the childValue object
          if(childValue.IsMap()) {
            // Iterate through it, if you like:
            for (auto it = childValue.begin(); it != childValue.end(); ++it) {
              // ...
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我实际上想要一个包含键和值的子节点,这样我就可以将该节点传递给另一个函数,而不是单独发送值。我不使用迭代器的键和值创建新节点,而是将键和值作为参考传递给子节点。
    • 我实际上想要一个包含键和值的子节点,这样我就可以将节点的引用传递给另一个函数,而不是单独发送值。我不想用迭代器的键和值创建一个新节点,而是传递节点的引用。
    • @user3389312,根据您的图结构,没有这样的节点(没有“键/值”节点,只有键节点和值节点)。为什么不能将两者都传递给你的函数?
    • 如果您不想同时传递两者,那么您可以重组 YAML,例如,将其转换为序列,并将键移动到名为 type 的字段或其他内容。跨度>
    • 取消引用 Yaml 迭代器会提供一个 yaml 节点,对吗?这是否意味着 YAML::Map 不是单个节点对象?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多