private static < E extends Element > List < E > findTopLevelElementsRecurse ( Class < E > elementType , Node node , List < E > matches ) { for ( Element elem : node . getChildElements ( ) ) { if ( elementType . isInstance ( elem ) ) { // Found match if ( matches == null ) matches = new ArrayList <> ( ) ; matches . add ( elementType . cast ( elem ) ) ; } else { // Look further down the tree matches = findTopLevelElementsRecurse ( elementType , elem , matches ) ; } } return matches ; }
Recursive component of findTopLevelElements .