Parsing through yaml

872 views
Skip to first unread message

Phil

unread,
Jun 27, 2011, 12:18:36 PM6/27/11
to SnakeYAML
Just started yaml the other day, and I'm still not understanding how
to parse a yaml document. From what it seems from the snakeyaml
documentation, yaml nodes are meant to be converted into straight java
objects. I was expecting it to be more similar to an xml approach,
where I read through the nodes, then parse through the children of
those nodes. Can someone give me a brief explanation as to how I'm
supposed to be looking at this? I basically just wanted the document
to be a configuration file, with some more complex settings.

thanks for your time,
Phil

Andrey

unread,
Jun 27, 2011, 12:36:09 PM6/27/11
to snakeya...@googlegroups.com
Hi Phil,
first you may check the validity of your document here:

Then try this:
Yaml yaml = new Yaml();
Object obj = yaml.load(<Your input as String or InputStream or Reader>);
System.out.println(obj);
See what comes out :)

You may also take the source and check the examples folder:


Tests contain many more examples. 

-
Andrey

Phil

unread,
Jun 27, 2011, 1:01:51 PM6/27/11
to SnakeYAML
Ah, the examples folder is nice. From what I gather, the correct way
to parse then would be to cast the object as a Map<String, Object>,
then iterate through the mapped nodes parsing as necessary?

On Jun 27, 11:36 am, Andrey <py4...@gmail.com> wrote:
> Hi Phil,
> first you may check the validity of your document here:http://instantyaml.appspot.com/
>
> Then try this:
>
> Yaml yaml = new Yaml();
> Object obj = yaml.load(<Your input as String or InputStream or Reader>);
> System.out.println(obj);
>
> See what comes out :)
>
> You may also take the source and check the examples folder:
>
> http://code.google.com/p/snakeyaml/source/browse/#hg%2Fsrc%2Ftest%2Fj...

Andrey Somov

unread,
Jun 27, 2011, 3:17:22 PM6/27/11
to snakeya...@googlegroups.com
Yes, this is one of the ways.
You may also have a look at examples for
http://code.google.com/p/snakeyaml/source/browse/src/main/java/org/yaml/snakeyaml/JavaBeanLoader.java
if your root object is a JavaBean

-
Andrey

Phil

unread,
Jun 27, 2011, 4:09:28 PM6/27/11
to SnakeYAML
Well, that got me over the elementry learning hump. SnakeYaml is
incredibly nice, the only other time I used it was during a java play
application - all I needed was to write out the config file properly
though, didn't need to write any code.

Have a good one and thanks again,
Phil

On Jun 27, 2:17 pm, Andrey Somov <py4...@gmail.com> wrote:
> Yes, this is one of the ways.
> You may also have a look at examples forhttp://code.google.com/p/snakeyaml/source/browse/src/main/java/org/ya...

Takidoso

unread,
Jun 28, 2011, 6:12:42 AM6/28/11
to SnakeYAML
Hi Phil,
YAML-processors are mostly more comparable to DOM-parser than to SAX
or Stax.
You just tell SnakeYaml to load your documents, while more than on
document can be in one physical file.
You could do something like this:

Yaml yaml = new Yaml();
configs = yaml.loadAll(configContents);

while configContentscould be a yaml string, an InputStream or a Reader
configs are, so far I remember correctly, the Iterable of the
particular Yaml-Documents. If you have more than one you you could use
a loop.

You have 2 different ways how your configuration file shows up. Either
as standard Java-Objects or you use your own Objects that work very
much like beans.

good luck and have fun

:-)

Andrey

unread,
Jun 28, 2011, 6:35:22 AM6/28/11
to snakeya...@googlegroups.com
SnakeYAML does the same as Spring does with XML - it creates business objects

The API similar to what DOM is for XML can be used via Nodes.
Check the architecture:

The  API similar to what SAX is for XML can be used via Events.

The internal structure can be investigated here: (switch on nodes or events to see the details)

-
Andrey
Reply all
Reply to author
Forward
0 new messages