简体   繁体   中英

Parsing YAML file using yaml-cpp

I am using yaml-cpp 0.3.0 (old API version) to parse a yaml file (which I got from here ).

This is the yaml file:

- name: Ogre
  position: [0, 5, 0]
  powers:
    - name: Club
      damage: 10
    - name: Fist
      damage: 8
- name: Dragon
  position: [1, 0, 10]
  powers:
    - name: Fire Breath
      damage: 25
    - name: Claws
      damage: 15
- name: Wizard
  position: [5, -3, 0]
  powers:
    - name: Acid Rain
      damage: 50
    - name: Staff
      damage: 3

And now I try to parse the powers name and damage with this code:

#include "yaml-cpp/yaml.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// our data types
struct Vec3 {
   float x, y, z;
};

struct Power {
   std::string name;
   int damage;
};

struct Monster {
   std::string name;
   Vec3 position;
   std::vector <Power> powers;
};

// now the extraction operators for these types
void operator >> (const YAML::Node& node, Vec3& v) {
   node[0] >> v.x;
   node[1] >> v.y;
   node[2] >> v.z;
}

void operator >> (const YAML::Node& node, Power& power) {
   node["name"] >> power.name;
   node["damage"] >> power.damage;
}

void operator >> (const YAML::Node& node, Monster& monster) {
   node["name"] >> monster.name;
   node["position"] >> monster.position;
   const YAML::Node& powers = node["powers"];
   for(unsigned i=0;i<powers.size();i++) {
      Power power;
      powers[i] >> power;
      monster.powers.push_back(power);
   }
}

int main()
{
   std::ifstream fin("robot.yaml");
   YAML::Parser parser(fin);
   YAML::Node doc;
   parser.GetNextDocument(doc);
   for(unsigned i=0;i<doc.size();i++) {
      Power power;
      doc[i] ["powers"] >> power;
      std::cout << power.name << "\n";
      std::cout << power.damage << "\n";
   }

   return 0;
}

It seems that my code shows error like this:

terminate called after throwing an instance of 'YAML::TypedKeyNotFound<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
  what():  yaml-cpp: error at line 4, column 5: key not found: name
Aborted (core dumped)

Please help me understand why can't I parse the powers element.

And please explain to me how can I parse all the key & values in one code since I am new with coding.

So the problem is here:

  doc[i] ["powers"] >> power;

doc[i]["powers"] does not have an index "name":

  • name: Ogre
    position: [0, 5, 0]
    powers:
    • name: Club
      damage: 10
    • name: Fist
      damage: 8

The "-" denotes items. So similarly to doc[i] , you would need to index firstly:

Power power;
doc[i]["powers"][0] >> power;
std::cout << power.name //outputs "Club"

But I assume you want all powers for each item, so you would need another explicit for-loop:

for (unsigned i = 0; i < doc.size(); i++)
{
    for (unsigned j = 0; j < doc[i]["powers"].size(); ++j)
    {
        Power power;
        doc[i]["powers"][j] >> power;
        std::cout << power.name << "\n";
        std::cout << power.damage << "\n";
    }
}

But that's a bit overkill. Better use your Monster -class directly, since the power values are already stored there:

for(unsigned i=0;i<doc.size();i++) {
    Monster monster;
    doc[i] >> monster;

    for (auto& power : monster.powers) // range-for loop
    {
        std::cout << power.name << "\n";
        std::cout << power.damage << "\n";
    }
    std::cout << monster.name << "\n";
}

Maybe this is a good start. A continuation of the answer would depend on what you want to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM