source class: File or URL
helper class: InputStreamReader, InputSource,SAXParserFactory
core class: your handler extends DefaultHandler , saxParser.
core method: saxParser.parse(); DefaultHandler.startElement()
package
test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.gnu.stealthp.rsslib.RSSException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/** */ /**
* PS:
*
* @author <a href="mailto:chenjingjing@hotmail.com">chen qingqing</a>
* @since 2007-10-25 上午11:02:44
*/
public class MyRss ... {
public static void main(String[] args) throws RSSException, IOException,
ClassCastException, ClassNotFoundException, IllegalAccessException,
InstantiationException, SAXException, ParserConfigurationException ...{
URL url = new URL("http://xml.weather.yahoo.com/forecastrss?p=33145&u=c"); // 1. source
InputStreamReader isr = new InputStreamReader(url.openStream());
InputSource is = new InputSource(isr);
MyHandler handler = new MyHandler(); // 2. handler
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser(); // 3. parser
saxParser.parse(is,handler); //4. parse
System.out.println(handler.weatherChannel.humidity);
System.out.println(handler.weatherChannel.windSpeed);
}
}
class MyHandler extends DefaultHandler ... {
public WeatherChannel weatherChannel = new WeatherChannel(); // Data transfer object
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException ...{
super.startElement(uri, localName, qName, attributes);
if (qName.equals("yweather:wind")) ...{
weatherChannel.windSpeed = attributes.getValue(2);
}
if (qName.equals("yweather:atmosphere")) ...{
weatherChannel.humidity = attributes.getValue(0);
}
if (qName.equals("yweather:condition")) ...{
WeatherItem item = new WeatherItem();
item.setText(attributes.getValue(0));
item.setCode(Integer.parseInt(attributes.getValue(1)));
item.setHigh(Integer.parseInt(attributes.getValue(2)));
item.setLow(Integer.parseInt(attributes.getValue(2)));
item.setDate(attributes.getValue(3));
item.setDay("今天");
weatherChannel.addItem(item);
}
if (qName.equals("yweather:forecast")) ...{
WeatherItem item = new WeatherItem();
item.setText(attributes.getValue(4));
item.setCode(Integer.parseInt(attributes.getValue(5)));
item.setHigh(Integer.parseInt(attributes.getValue(2)));
item.setLow(Integer.parseInt(attributes.getValue(3)));
item.setDate(attributes.getValue(1));
item.setDay(attributes.getValue(0));
weatherChannel.addItem(item);
}
}
}
class WeatherChannel ... {
public String windSpeed, humidity;
List<WeatherItem> weatherItemList = new java.util.ArrayList<WeatherItem>();
public String toXML() ...{
return null;
}
public void addItem(WeatherItem weatherItem) ...{
weatherItemList.add(weatherItem);
}
public WeatherItem getItem(int index) ...{
if (index > itemSize())
return null;
return weatherItemList.get(index);
}
public int itemSize() ...{
return weatherItemList.size();
}
}
class WeatherItem ... {
String day;
String text;
int code, low, high;
String date;
public String toXML() ...{
return null;
}
public int getCode() ...{
return code;
}
public void setCode(int code) ...{
this.code = code;
}
public String getDate() ...{
return date;
}
public void setDate(String date) ...{
this.date = date;
}
public String getDay() ...{
return day;
}
public void setDay(String day) ...{
this.day = day;
}
public int getHigh() ...{
return high;
}
public void setHigh(int high) ...{
this.high = high;
}
public int getLow() ...{
return low;
}
public void setLow(int low) ...{
this.low = low;
}
public String getText() ...{
return text;
}
public void setText(String text) ...{
this.text = text;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.gnu.stealthp.rsslib.RSSException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/** */ /**
* PS:
*
* @author <a href="mailto:chenjingjing@hotmail.com">chen qingqing</a>
* @since 2007-10-25 上午11:02:44
*/
public class MyRss ... {
public static void main(String[] args) throws RSSException, IOException,
ClassCastException, ClassNotFoundException, IllegalAccessException,
InstantiationException, SAXException, ParserConfigurationException ...{
URL url = new URL("http://xml.weather.yahoo.com/forecastrss?p=33145&u=c"); // 1. source
InputStreamReader isr = new InputStreamReader(url.openStream());
InputSource is = new InputSource(isr);
MyHandler handler = new MyHandler(); // 2. handler
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser(); // 3. parser
saxParser.parse(is,handler); //4. parse
System.out.println(handler.weatherChannel.humidity);
System.out.println(handler.weatherChannel.windSpeed);
}
}
class MyHandler extends DefaultHandler ... {
public WeatherChannel weatherChannel = new WeatherChannel(); // Data transfer object
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException ...{
super.startElement(uri, localName, qName, attributes);
if (qName.equals("yweather:wind")) ...{
weatherChannel.windSpeed = attributes.getValue(2);
}
if (qName.equals("yweather:atmosphere")) ...{
weatherChannel.humidity = attributes.getValue(0);
}
if (qName.equals("yweather:condition")) ...{
WeatherItem item = new WeatherItem();
item.setText(attributes.getValue(0));
item.setCode(Integer.parseInt(attributes.getValue(1)));
item.setHigh(Integer.parseInt(attributes.getValue(2)));
item.setLow(Integer.parseInt(attributes.getValue(2)));
item.setDate(attributes.getValue(3));
item.setDay("今天");
weatherChannel.addItem(item);
}
if (qName.equals("yweather:forecast")) ...{
WeatherItem item = new WeatherItem();
item.setText(attributes.getValue(4));
item.setCode(Integer.parseInt(attributes.getValue(5)));
item.setHigh(Integer.parseInt(attributes.getValue(2)));
item.setLow(Integer.parseInt(attributes.getValue(3)));
item.setDate(attributes.getValue(1));
item.setDay(attributes.getValue(0));
weatherChannel.addItem(item);
}
}
}
class WeatherChannel ... {
public String windSpeed, humidity;
List<WeatherItem> weatherItemList = new java.util.ArrayList<WeatherItem>();
public String toXML() ...{
return null;
}
public void addItem(WeatherItem weatherItem) ...{
weatherItemList.add(weatherItem);
}
public WeatherItem getItem(int index) ...{
if (index > itemSize())
return null;
return weatherItemList.get(index);
}
public int itemSize() ...{
return weatherItemList.size();
}
}
class WeatherItem ... {
String day;
String text;
int code, low, high;
String date;
public String toXML() ...{
return null;
}
public int getCode() ...{
return code;
}
public void setCode(int code) ...{
this.code = code;
}
public String getDate() ...{
return date;
}
public void setDate(String date) ...{
this.date = date;
}
public String getDay() ...{
return day;
}
public void setDay(String day) ...{
this.day = day;
}
public int getHigh() ...{
return high;
}
public void setHigh(int high) ...{
this.high = high;
}
public int getLow() ...{
return low;
}
public void setLow(int low) ...{
this.low = low;
}
public String getText() ...{
return text;
}
public void setText(String text) ...{
this.text = text;
}
}