Ticket #6866: TestXPath.java

File TestXPath.java, 1.3 KB (added by jhuntley, 14 years ago)
Line 
1/**
2 *
3 */
4import java.io.InputStream;
5import java.net.URL;
6import java.net.URLConnection;
7
8import javax.xml.parsers.DocumentBuilder;
9import javax.xml.parsers.DocumentBuilderFactory;
10import javax.xml.xpath.XPath;
11import javax.xml.xpath.XPathFactory;
12
13import org.w3c.dom.Document;
14
15/**
16 * @author Jason Huntley
17 *
18 */
19public class TestXPath {
20 private static String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU";
21 private static String imageUrlTemplate;
22 /**
23 * @param args
24 */
25 public static void main(String[] args) {
26 try {
27 URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key="
28 + API_KEY);
29 URLConnection conn = u.openConnection();
30
31 InputStream stream = conn.getInputStream();
32
33 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
34 DocumentBuilder builder = factory.newDocumentBuilder();
35 Document document = builder.parse(stream);
36
37 XPathFactory xPathFactory = XPathFactory.newInstance();
38 XPath xpath = xPathFactory.newXPath();
39 imageUrlTemplate = xpath.compile("//ImageryMetadata/ImageUrl/text()").evaluate(document);
40 System.out.println(imageUrlTemplate);
41 } catch(Exception e) {
42 System.out.println(e.getMessage());
43 e.printStackTrace();
44 }
45 }
46
47}