Extract hierarchy / tree of products

I want to import a STEP-file using OpenCascade. The file comes from a CAD program, in which a hierarchy of components is represented, e.g., I have a root component, underneath several other components, and under those several sub-components, and so forth.

With the following code, I am able to get all instance and product names of all components in the STEP-file, but I am not able to figure out, which component is a child/parent to another component in the CAD tree structure.

Handle(XCAFDoc_ShapeTool) assembly = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main());
TDF_LabelSequence freeShapes;
assembly->GetFreeShapes(freeShapes);
            
TDF_LabelSequence shapes;
assembly->GetShapes(shapes);

for (Standard_Integer freeShapeIndex = 1; freeShapeIndex <= freeShapes.Length(); ++freeShapeIndex)
{
   TDF_Label freeShape = freeShapes.Value(freeShapeIndex);

   TDF_LabelSequence components;
   assembly->GetComponents(freeShape, components, Standard_True);

   for (Standard_Integer compIndex = 1; compIndex <= components.Length(); ++compIndex)
   {
      TDF_Label compLabel = components.Value(compIndex);
      Handle(TDataStd_Name) instance, product;
      TCollection_ExtendedString nameOfInstance, nameOfProduct;
      if (compLabel.FindAttribute(TDataStd_Name::GetID(), instance))
      {
         nameOfInstance = instance->Get();
      }

      TDF_Label productShape;
      XCAFDoc_ShapeTool::GetReferredShape(compLabel,productShape);
      if ( productShape.FindAttribute(TDataStd_Name::GetID(),product) )
      {
        nameOfProduct= product->Get();
      }
   }
}

I want to obtain some kind of list (the specific data structure is not important for now), which gives me the parent to each of the components. It should store the product name (string) of the child and parent. I get the components using the code above, but how can I get the parent-child relationship?

Thanks for your help :)