http://www.google.com/ig/api?hl=zh_cn&weather=hangzhou
怎样获取上面连接的当天天气的值

解决方案 »

  1.   

    测试数据就是你给的。
    一种是dom4j,一种是正则。import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.dom4j.Document;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
    public class GetXmlText { public static void main(String[] args) throws Exception{
    getTextByDom4j();
    getTextByRegex();
    }


    public  static void getTextByDom4j() throws Exception{
    SAXReader reader = new SAXReader();    
    Document doc = reader.read(new File("c:\\api.xml"));
    List lowNodes = doc.selectNodes("//weather/forecast_conditions/low");    
    for(Object obj:lowNodes) {    
    Node lowNode = (Node)obj;
    String value = lowNode.valueOf("@data");    
    System.out.println("low:"+value);
    }
    List highNodes = doc.selectNodes("//weather/forecast_conditions/high");    
    for(Object high:highNodes){
    Node highNode = (Node)high;
    String value = highNode.valueOf("@data");    
    System.out.println("high:"+value);
    }

    }

    public static void getTextByRegex() throws Exception{
    BufferedReader br=new BufferedReader(new FileReader("c:\\api.xml"));
    String str=null;
    String regex="((low)|(high))\\s*data=\\s*\"(\\d+)\"";
    while((str=br.readLine())!=null){
    Pattern p=Pattern.compile(regex);
    Matcher m=p.matcher(str);
    while(m.find()){
    System.out.println(m.group(1)+":"+m.group(4));
    }
    }
    br.close();
    }


    }
      

  2.   

    http://blog.csdn.net/yan54226522/archive/2009/05/09/4162937.aspx
      

  3.   

    这是一个网络存储的吧
    前面介绍的几种存储都是将数据存储在本地设备上,除此之外,还有一种存储(获取)数据的方式,通过网络来实现数据的存储和获取,下面看一个在Android上调用WebService的例子。 注意
     在Android的早期版本中,曾经支持过进行XMPP Service和Web Service的远程访问。Android SDK 1.0以后的版本对它以前的API作了许多的变更。Android 1.0以上版本不再支持XMPP Service,而且访问Web Service的API全部变更。
     1.例子介绍
    通过邮政编码查询该地区的天气预报,以POST发送的方式发送请求到webservicex.net站点,访问WebService.webservicex.net站点上提供查询天气预报的服务,具体信息请参考其WSDL文档,网址为:http://www.webservicex.net/WeatherForecast.asmx?WSDL。输入:美国某个城市的邮政编码。 输出:该邮政编码对应城市的天气预报。2.实现步骤如下
    (1)如果需要访问外部网络,则需要在AndroidManifest.xml文件中加入如下代码申请权限许可: <!-- Permissions --> <uses-permission Android:name="Android.permission.INTERNET" /> (2)以HTTP POST的方式发送(注意:SERVER_URL并不是指WSDL的URL,而是服务本身的URL)。实现的代码如下所示:private static final String SERVER_URL = "http://www.webservicex.net/WeatherForecast. asmx/GetWeatherByZipCode"; //定义需要获取的内容来源地址HttpPost request = new HttpPost(SERVER_URL); //根据内容来源地址创建一个Http请求// 添加一个变量 List <NameValuePair> params = new ArrayList <NameValuePair>(); // 设置一个华盛顿区号params.add(new BasicNameValuePair("ZipCode", "200120"));  //添加必须的参数request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //设置参数的编码try { HttpResponse httpResponse = new DefaultHttpClient().execute(request); //发送请求并获取反馈// 解析返回的内容if(httpResponse.getStatusLine().getStatusCode() != 404)  {   String result = EntityUtils.toString(httpResponse.getEntity());   Log.d(LOG_TAG, result); } } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); }  代码解释:如上代码使用Http从webservicex获取ZipCode为“200120”(美国WASHINGTON D.C)的内容,其返回的内容如下:<WeatherForecasts xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns="http://www.webservicex.net">   <Latitude>38.97571</Latitude>   <Longitude>77.02825</Longitude>   <AllocationFactor>0.024849</AllocationFactor>   <FipsCode>11</FipsCode>   <PlaceName>WASHINGTON</PlaceName>   <StateCode>DC</StateCode>   <Details>     <WeatherData>       <Day>Saturday, April 25, 2009</Day>       <WeatherImage>http://forecast.weather.gov/images/wtf/sct.jpg</WeatherImage>       <MaxTemperatureF>88</MaxTemperatureF>       <MinTemperatureF>57</MinTemperatureF>       <MaxTemperatureC>31</MaxTemperatureC>       <MinTemperatureC>14</MinTemperatureC>     </WeatherData>     <WeatherData>       <Day>Sunday, April 26, 2009</Day>       <WeatherImage>http://forecast.weather.gov/images/wtf/few.jpg</WeatherImage>       <MaxTemperatureF>89</MaxTemperatureF>       <MinTemperatureF>60</MinTemperatureF>       <MaxTemperatureC>32</MaxTemperatureC>       <MinTemperatureC>16</MinTemperatureC>     </WeatherData>…  </Details> </WeatherForecasts>这个例子演示了如何在Android中通过网络获取数据,掌握该类内容,开发者需要熟悉java.net.*,Android.net.*这两个包的内容,在这就不赘述了,请读者参阅相关文档。
      

  4.   

    请问如果不是本地的xml,直接用url这个程序应该怎么改写的?
      

  5.   

    第一种方法报错的
    Exception in thread "Main Thread" org.dom4j.DocumentException: Error on line 2 of document file:///C:/web.xml : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.dom4j.io.SAXReader.read(SAXReader.java:264)
    at test.Test1.getTextByDom4j(Test1.java:24)
    at test.Test1.main(Test1.java:18)
    Nested exception: 
    org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1038)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.dom4j.io.SAXReader.read(SAXReader.java:264)
    at test.Test1.getTextByDom4j(Test1.java:24)
    at test.Test1.main(Test1.java:18)
      

  6.   

    我把你的格式保存成xml没有问题。
    如果是rul形式可以通过流的方式读取下来。直接通过正则解析就行了。