kohaNimi = "Tallinn";
URL myUrl = new URL("http://maps.googleapis.com/maps/api/geocode/xml?address=" + kohaNimi + "&sensor=false");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(myUrl.openStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/GeocodeResponse/result/bounds/southwest/lat");
String swLat = expr.evaluate(doc, XPathConstants.STRING).toString();
System.out.println("swLat: " + swLat);
因此,我嘗試使用Google地理編碼API獲取城鎮的座標,但我無法解析xml。我正在嘗試與Java一起使用xPath。我可以證實......確實存在。問題是,當我試圖解析這個XML我不會得到任何文字作爲迴應。我查看了代碼,似乎無法弄清楚什麼是錯的。我用這個博客帖子組裝我的代碼:http://tunatore.wordpress.com/2011/05/20/how-to-use-xpath-i-java-simple-example/使用Google Geocode在Java中使用XPath解析XML
您可以驗證XPath是正確的(應該得到響應)從這裏:http://maps.googleapis.com/maps/api/geocode/xml?address=Tallinn&sensor=false
任何人都可以發現有什麼不好?
什麼是你的實際輸出? –
我檢查了指定的網址(Talinn&...)。返回的XML結構在GeocodeRespoonse/result標籤中沒有'bounds'元素。 – zaerymoghaddam