The GAPS programme with HARPS-N at TNG. V. A comprehensive analysis of the XO-2 stellar and planetary systems