Ways of loading multiple YAML Documents in a single file.

3,282 views
Skip to first unread message

Abhishek Kona

unread,
Jun 23, 2011, 8:11:28 AM6/23/11
to snakeya...@googlegroups.com
Hi

Is it possible to load multiple YAML documents in a single file with a
named reference in Snake YAML.

e.g.:

---NM
host1 = machine1
host2 = machine2
---local
host1 = localhost
host2 = localhost

Can I load the document for "NM" or "local" on choice.

-Abhishek Kona

Andrey

unread,
Jun 23, 2011, 8:27:56 AM6/23/11
to snakeya...@googlegroups.com
Hi,
if you wish to work with YAML I would recommend to browse the specification:

YAML does not support "named" documents.

-
Andrey

Jordan Angold

unread,
Jun 23, 2011, 11:24:28 AM6/23/11
to SnakeYAML
As Andrey mentions, there's no such thing as a "named" document in
YAML. However, there are a few ways to do this.

First, you could choose the documents from separate files, so the
documents are "named" by the filename. You would have NM.yaml and
local.yaml (or whatever).

Second, you could load both documents from a single file as a mapping,
then choose the one you wanted:

---
NM:
host1 : machine1
host2 : machine2
local:
host1 : localhost
host2 : localhost
...

Then the Java code, assuming each of your "documents" corresponds to a
class named "Settings":

Yaml yaml = new Yaml();
Map<String,Settings> settingsMap = (Map<String,Settings>)yaml.load
( inputStream );

Settings localSettings = settingsMap.get ( "local" );
Settings nmSettings = settingsMap.get ( "NM" );

/Jordan

P.S. YAML doesn't use '=', it uses ':'.
Reply all
Reply to author
Forward
0 new messages