Our first example uses Traffic Web Services from Yahoo. The Traffic Web Services from Yahoo! enable you to access real-time traffic alerts information for a given location.
We will use Traffic Web Services - Traffic REST API. REST stands for Representational State Transfer. For more information, check Representational State Transfer in the Wikipedia or Yahoo! Web Service FAQ on REST.
You don't have to worry about what REST is. The only think you need to know is by sending a URL request, you get back a response in XML formet.
Let's see what happens in Beverly Hill (zip code 90210). Here is the URL I use:
http://api.local.yahoo.com/MapsService/V1/trafficData?appid=charlesli_test1&zip=90210
This is a sample response (on March 1, 2006, the response may be different at a different time.).
The responses are in XML format. You can use built-in Java XML libraries to process the file. Here is our first example.
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /* * YahooTest1.java * * Created on March 1, 2006, 1:30 PM */ /** * @author Charles Li * Copyright (c) 2006 All Rights Reserved. */ public class YahooTest1 { public static void main(String[] args) throws Exception { // the URL String query = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=charlesli_test1&zip=90210"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // parse the response Document document = builder.parse(query); // get the tag with ID="Title" NodeList list = document.getElementsByTagName("Title"); // runs through all tags with Title for(int i=0; i < list.getLength(); i++) { Node node = list.item(i); // print out the content System.out.println(node.getTextContent()); } } }
Road construction, on I-405 at SANTA MONICA BLVD Road construction, on I-405 at SAWTELLE BLVD Road construction, on I-405 at SUNSET BLVD Paving operations, on US-101 at HOLLYWOOD FWY Road construction, on US-101 at WOODMAN AVE Road construction, on US-101 at COLDWATER CANYON AVE Road construction, on I-405 at VENTURA BLVDThe output is because
<?xml version="1.0" encoding="UTF-8"?> ...... <Result type="construction"> <Title>Road construction, on I-405 at SANTA MONICA FWY</Title> <Description> MAINLINE CONSTRUCTION 1 LANE S CLOSED FOR K RAIL INSTALLATION,STRIPING </Description> ...... </Result> <Result type="construction"> <Title>Road construction, on I-10 at MANNING AVE</Title> ...... </Result> <Result type="construction"> <Title>Road construction, on I-405 at SANTA MONICA BLVD</Title> ...... </Result> <Result type="construction"> <Title>Road construction, on I-405 at SAWTELLE BLVD</Title> ...... </Result> <Result type="construction"> <Title>Road construction, on I-405 at SUNSET BLVD</Title> ...... </Result> <Result type="construction"> <Title>Paving operations, on US-101 at HOLLYWOOD FWY</Title> ...... </Result> <Result type="incident"> <Title>Incident, on FRANKLIN AVE at TAMARIND AVE</Title> ...... </Result> <Result type="construction"> <Title>Road construction, on US-101 at WOODMAN AVE</Title> ...... </Result> <Result type="construction"> <Title> Road construction, on US-101 at COLDWATER CANYON AVE</Title> ...... </Result> <Result type="construction"> <Title>Road construction, on I-405 at VENTURA BLVD</Title> ...... </Result> </ResultSet> ......
query
: is the URL that we used.DocumentBuilderFactory factory
, DocumentBuilder builder
:
standard procedure to create a document
builder.parse(query)
: parse the URL for further processing. NodeList list = document.getElementsByTagName("Title");
:
get those with Title
tag.
See Java 1.5.0 API
on Document
interface