According to the information on the Chinese Embassy's site, Visas are not required of aliens (except citizens of the United States and the United Kingdom), who hold final destination tickets and have booked seats on international airliners flying directly through China, and will stay in a transit city for less than 24 hours without leaving the airport.
If you are a U.S. or U.K. citizen, you will need a visa. You must apply for the visa in person at the nearest consular office. You will need:
This link to the Shanghai airport may also be of some use to you.