6

Is there a way to get the name of a node? For example:

Fruits:
   - apple
   - orange
   - pear

and in C++:

YAML::Node fruit = parser["Fruits"];
cout << fruit.name() << endl;  // should print "Fruits"

is there something like YAML::Node::name()? I don't see anything in Node.h that fits the bill.

If there isn't, any suggestions on a simple way to modify the code to record this information?

Thanks!

user3416593
  • 201
  • 3
  • 8

2 Answers2

2

What you're really looking for is the key associated with a value in a map. You're right, there's no link back from a value to its key, but you can store one, when you're navigating the node in the first place.

If all of your keys are string keys, then whenever you pass a value to some function, just pass along the string key as well:

// Instead of:
doSomething(node[key]);

// Call:
doSomething(key, node[key]);
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • 3
    It would be really nice for the parser to have this information embedded in it so that user code didn't have to keep track of the hierarchy for error trapping purposes. – user3416593 Mar 27 '14 at 16:34
  • 2
    If the parser doesn't keep this information then I must embed most YAML calls in try/catch blocks with my own error handling or other similarly invasive error trapping. This adds a lot of overhead to the user code. – user3416593 Mar 27 '14 at 17:04
  • Please file a feature request on the project home page, if you feel like this is important. – Jesse Beder Mar 27 '14 at 22:19
0
Fruits:
   - apple
   - orange
   - pear

This is one key-value-pair

"Fruits" is the key

" -apple -orange -pear " is the value

YAML::Node fruit = parser["Fruits"];

This command use one key to query the value So the node "fruit" just keep information about value.

Rubik
  • 33
  • 5