SAX notes
来源:互联网 发布:视频压缩算法工程师 编辑:程序博客网 时间:2024/06/06 01:26
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;
}
}
- SAX notes
- Notes
- notes
- Notes
- notes
- notes
- notes
- notes
- Notes
- notes
- Notes
- notes
- Notes
- notes
- Notes
- Notes
- Notes
- notes
- Java 语言编码规范
- 上网行为管理系统
- 转:ASP.net中动态加载控件时一些问题的总结
- Tomcat配置SQLServer数据连接池
- 农业银行代销基金一览表
- SAX notes
- DTD-属性
- C#程序设计(二)-----Windows窗体及标签控件
- [原创]ASP.NET 运行模式学习(一)
- Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a
- cakephp分页/cakephpページング/cakephp paging
- 开发笔记:Java命名规范
- DTD - 实体(Entities)
- 做程序员最重要的是什么?