ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

Based on this StackOverflow Q&A, you can use this piece of code:

YAML::Node root = /* ... */;
YAML::Node node = root["Node"];

// We now have a map node, so let's iterate through:
for (auto it = node.begin(); it != node.end(); ++it) {
  YAML::Node key = it->first;
  YAML::Node value = it->second;
  if (key.Type() == YAML::NodeType::Scalar) {
    // This should be true; do something here with the scalar key.
  }
  if (value.Type() == YAML::NodeType::Map) {
    // This should be true; do something here with the map.
  }
}

to parse nested YAML like this one below

Node:
    Foo:
      f1 : one
      f2 : two
    Bar:
      b1 : one
      b2 : two

Based on this StackOverflow Q&A, you can use this piece of code:

YAML::Node root = /* ... */;
YAML::Node node = root["Node"];

// We now have a map node, so let's iterate through:
for (auto it = node.begin(); it != node.end(); ++it) {
  YAML::Node key = it->first;
  YAML::Node value = it->second;
  if (key.Type() == YAML::NodeType::Scalar) {
    // This should be true; do something here with the scalar key.
  }
  if (value.Type() == YAML::NodeType::Map) {
    // This should be true; do something here with the map.
  }
}

to parse nested YAML like this one below

Node:
    Foo:
      f1 : one
      f2 : two
    Bar:
      b1 : one
      b2 : two

Other nice SO answer is here - code snippet below.

This loop:

for (YAML::const_iterator ti = attributes.begin(); ti !=

attributes.end(); ++ti)

is iterating over a map node. Therefore, the iterator points to a key/value pair. Your next line:

const YAML::Node& frame = *ti;

dereferences it as a node. Instead, you need to look at its key/value nodes:

const YAML::Node& key = ti->first;
const YAML::Node& value = ti->second;

yaml-cpp allows iterators to both point to nodes and key/value pairs because it can be a map or a sequence (or a scalar), and it's implemented as a single C++ type.