# Finding all RShape children recursively (Geomerative)

I have an svg file with multiple levels of hierarchy. `<g>` within `<g>`, and different elements such as `<polygon>`, `<polyline>`, etc within each group. My goal is to create a function that will return me all children elements as `RShape`s in a simple single array format.

I think I will need to do this recursively by creating a function:

``````RShape[] getAllChildren(RShape shp) { }
``````

and check whether `shp` has any children, and if so, run the function recursively until there’s no child left to find:

``````int numChildren = shp.countChildren();
if (numChildren != 0) {
// run getAllChildren() on each child
} else {
// return RShape
}
``````

Then, return each shape one level up, store them all in a single array and return it. It sort of makes sense to me conceptually but I’m having difficulty implementing it.

I know that there are some recursion examples like drawing a tree, but I feel just running functions like `line()` recursively and having to return some data and track them like I’m trying here are somewhat different.

If you have any example or suggestions, I’d appreciate it.

Tried a few different things and came up with this function:

``````RShape[] getAllChildren(RShape shp) {
ArrayList<RShape> result = new ArrayList<RShape>();
int numChildren = shp.countChildren();
if (numChildren != 0) {
for (int i = 0; i < numChildren; i++) {
RShape[] children = getAllChildren(shp.children[i]);
for (int j = 0; j < children.length; j++) {
}
}
} else {
}
return result.toArray(new RShape[result.size()]);
}
``````

I did a few tests and it seems working but if anyone has a better solution, please let me know!

can you give a link to an example svg? Or upload a file?

You are on a good path. Just go on.