I am a newbie to YAML and I want to parse the following yaml file :
basket :
size : 10
type : organic
fruit1:
mango : 5
type : farm-fresh
fruit2:
peach : 43
manufacturer : xyz
color : brown
design : netted
...
The yaml file will follow the above format, with any random string name and values(string, float, int, etc).
I want to store each of these values in a struct
, that has key
and values
as character array.
struct Input {
char key[100]:
char value[100];
};
There exists an array of the above struct to store the values from the yaml file.
So the data from the yaml files should be stored as:
//Input[x].key //Input[x].value
basket.size 10
basket.fruit1.mango 5
basket.fruit2.manufacturer xyz
basket.color brown
basket.desgin netted
I wrote an application to parse the yaml file, and I get individual nodes/leaves as an string output. So based on above yaml files, I get node values as basket
, size
, 5
, 43
, etc. I followed the approach as defined here. This is one of the good resource I found to learn yaml so far.
This approach is not that useful to me, since I do not have any relation between my previous nodes to the leaves and vice versa.
Does libyaml
provide a way to maintain this relationship in a tree and then give return in response to a query. I am bound to use libyaml
due to the project requirements. But any other suggestions would also be welcome.
The resource you linked describes several ways of parsing YAML. Token-based parsing, opposed to what the tutorial says, is not useful at all unless you are implementing a syntax highlighter. For all other cases, you want to use event-based parsing. So I'll assume you tried to use that.
Event-based parsing does maintain the tree structure (not sure what exactly you mean by relationship in a tree), you get …Start and …End events for sequences and mappings, which describe the input structure. It is quite straightforward to build a list of
struct Input
walking over the event stream: