⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forecast.java

📁 仿校内网站
💻 JAVA
字号:
package myTools;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Attributes;

public class forecast {
	
	private String[] c=null;
   
 
    public forecast() {
     
	 
	 super();
  // TODO Auto-generated constructor stub
 }
    public void sendRE() 
    {
     URL url;
	try {
		url = new URL("http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX0046");
		InputStream input = url.openStream();
	     SAXParserFactory factory = SAXParserFactory.newInstance();
	     factory.setNamespaceAware(false);
	     SAXParser parser = factory.newSAXParser();
	     parser.parse(input,new YahooHandler());
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
     
    }
    
    /**
     * For more information, please visit: 
     * Author: 小秦    

 */
    public class YahooHandler extends DefaultHandler {

        public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) 

    throws SAXException {
           if("yweather:forecast".equals(qName)) {
                String s_date = attributes.getValue(1);
                Date date = null;
                try {
                    date = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(s_date);
                }
                catch (Exception e) {
                    e.printStackTrace();
                    throw new SAXException("Cannot parse date: " + s_date);
                }
                int low = Integer.parseInt(attributes.getValue(2));
                int high = Integer.parseInt(attributes.getValue(3));
                String text = attributes.getValue(4);
                int code = Integer.parseInt(attributes.getValue(5));
                if(c==null)
                {  c=new String[3];
	                c[0]=text;
	                c[1]=low+"";
	                c[2]=high+"";
                }
               
                
            }
            super.startElement(uri, localName, qName, attributes);
       
        }
        
    }
   
    public String[] getC(){
    	return c;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -