2012-05-08 6 views
0

SAX를 사용하여 Google 날씨 API에서 정보를 가져오고 "조건"에 문제가 있습니다. , Google 날씨 API 조건

http://www.google.com/ig/api?weather=Boston+MA

한편 I은 현재 일에 대한 조건을 얻으려고 :

public void startElement (String uri, String name, String qName, Attributes atts) { 
    if (qName.compareTo("condition") == 0) { 
     String cCond = atts.getValue(0); 
     System.out.println("Current Conditions: " + cCond); 
     currentConditions.add(cCond); 
    } 

이 같은 것을에서 XML을 당겨하려면 :

특히이 코드를 사용하고 있습니다 미래의 어떤 날에도 적용되지 않습니다.

XML 파일의 내용을 기반으로 현재 하루 동안 데이터를 가져올 수있는 몇 가지 검사가 있습니까?

감사합니다.

+0

¿ SAX를 사용해야합니까? 완벽하게 가능하지만 XPath 작업과 비슷합니다. –

+0

색소폰은 이것을 분석하는 가장 쉬운 방법 인 것 같습니다. 나는 새로운 길을 배우기 위해 모두 다. –

+0

** Google 날씨 API는 2012 ** -> http://stackoverflow.com/questions/12145820/google-weather-api-gone/35943521에서 종료되었습니다. –

답변

1

트릭을해야합니다. 프레임 워크를 사용하고 있다면 XPath를위한 유틸리티 함수를 사용하면 더 간단하게 만들 수 있습니다.

import java.io.IOException; 
import org.w3c.dom.*; 
import org.xml.sax.SAXException; 
import javax.xml.parsers.*; 
import javax.xml.xpath.*; 

public class XPathWeather { 

    public static void main(String[] args) 
    throws ParserConfigurationException, SAXException, 
      IOException, XPathExpressionException { 

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = domFactory.newDocumentBuilder(); 
    Document doc = builder.parse("/tmp/weather.xml"); 

    XPathFactory factory = XPathFactory.newInstance(); 
    XPath xpath = factory.newXPath(); 
    XPathExpression expr = xpath.compile("/xml_api_reply/weather/current_conditions/condition/@data"); 

    String result = (String) expr.evaluate(doc, XPathConstants.STRING); 
    System.out.println(result); 
    } 

}