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:
- Your valid & actual passport. The passport must have at least six (6) months validity left before expiration and at least one blank page left in it.
- One completed Visa Application Form (Q1) with one additional passport photo (black & white or color is acceptable).
- A valid visa of the country of destination, and a final destination airline ticket. This final clause would seem to prevent you from purchasing an onward ticket in China.
This link to the Shanghai airport may also be of some use to you.