Problems with tree parsing: apply looping too many times on doc's elements