ExceptionHandler
ExceptionHandler

Reputation: 223

Yaml Parsing from a config file

This is the first time i am using a YAML parser and I am currently stuck at this point

I have a config file which goes something like

Users
 -Name:A
  Id : x
  Addr:10.0.0.1
 -Name:B
  Id  :y
  Addr:10.0.0.2

HomeAddress
 City:bla bla
 Country:bla bla

Office Address
 City:abchd
 Country:bha bha ba

So I thought the best way to parse it would be to have a list like this.

List<Map<String, obj>> Object = (List<Map<String, obj>>) yaml.load(input);

Objective was to access the object by specifying a string. Like Username A, I shld be able to obtain his id and IPAddr (This is the most important to me at the moment). But when I tried this declaration, I got an error like this

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
    at Message.MessagePasser.<init>(MessagePasser.java:34)

Can someone please help me debug this. I am running by a deadline!!:(

Upvotes: 2

Views: 13830

Answers (2)

Andrey
Andrey

Reputation: 3001

1) check the validity of your YAML here: http://instantyaml.appspot.com/

2) Your document should look like this: (mind the spaces !)

Users : 
 - Name : A
   Id : x
   Addr : 10.0.0.1
 - Name : B
   Id   : y
   Addr : 10.0.0.2

Upvotes: 1

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47923

The YAML parser seems to be returning a Map. So you should use it like this:

Map config = (Map) yaml.load(input);
Map usersConfig = config.get("Users");

Also what particular YAML parser are you using?

Update 1: If you look at the documentation, the load method either returns a List or Map depending on the contents of your YAML file. As your YAML file starts with a key-value mapping (Users) and not an array (-), the load method returns a Map which is the appropriate type to be returned in this case.

Upvotes: 3

Related Questions