Get all nodes under YAML path

HcgRandon picture HcgRandon · Mar 10, 2012 · Viewed 9.7k times · Source

I have a YAML file that looks like this:

Main:
  topofhouse:
    x: 276.4375
    y: 71.0
    z: -60.5
    yaw: -290.7768
    pitch: 35.400017
  2ndfloor:
    x: 276.5
    y: 67.0
    z: -60.5
    yaw: -8.626648
    pitch: 16.199997
  home:
    x: 276.5
    y: 63.0
    z: -60.5
    yaw: -18.976715
    pitch: -32.850002

Is there a way to get all nodes under Main?

Answer

r4vn picture r4vn · Mar 10, 2012

To get the node IDs contained in Main:

file.getConfigurationSection("Main").getKeys(false);

Output:

Set["topofhouse", "2ndfloor", "home"]

The ConfigurationSection.getConfigurationSection(String path) method is used to get the path on which to operate.

The ConfigurationSection.getKeys(boolean deep) method will get you all node IDs within the current path as a Set<String>. When deep is set to true, it will get all the nodes in the children and subchildren too, however, all relations between them will be lost.