Sorry can't answer your post fully particularly about trains etc. The main sites used by friends who come to Spain each year using the UK to Spain ferries are at la Cabrera to the north of Madrid and Aranjuez to the south. From the map there is a rail line shown from near the site into Madrid, distance is around 40 km.
You should be able to find these on-line. Also just google Madrid campsites.