TreeDS4j is a Java (6+) API that allows handling n-ary tree data structure in such a flexible way.
Indeed, the user is subject to enter structured and coherent data, and the latter would be stored in an hierarchy, accordingly.
The link below contains two samples that show how to take advantage of the API.