易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Android SAX解析XML
Android中有SAX、DOM及PULL等方式解析xml: SAX是一种占用内存少且解析速度快的解析器,它采用的是事件启动。官方推荐使用以下是通过网上 获取的天气xml文件
<?xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- published at 2011-12-14 14:55:05 -->
<Profiles>
<Weather>
<city>
广州
</city>
<status1>
多云
</status1>
<status2>
阴
</status2>
<figure1>
duoyun
</figure1>
<figure2>
yin
</figure2>
<direction1>
无持续风向
</direction1>
<direction2>
无持续风向
</direction2>
<power1>
≤3
</power1>
<power2>
≤3
</power2>
<temperature1>
20
</temperature1>
<temperature2>
12
</temperature2>
<ssd>
0
</ssd>
<tgd1>
21
</tgd1>
<tgd2>
21
</tgd2>
<zwx>
2
</zwx>
<ktk>
5
</ktk>
<pollution>
3
</pollution>
<xcz>
4
</xcz>
<zho
/>
<diy
/>
<fas
/>
<chy>
4
</chy>
<zho_shuoming>
暂无
</zho_shuoming>
<diy_shuoming>
暂无
</diy_shuoming>
<fas_shuoming>
暂无
</fas_shuoming>
<chy_shuoming>
套装、夹衣、风衣、夹克衫、西服套装、马甲衬衫配长裤
</chy_shuoming>
<pollution_l>
一般
</pollution_l>
<zwx_l>
弱
</zwx_l>
<ssd_l>
舒适
</ssd_l>
<fas_l>
暂无
</fas_l>
<zho_l>
暂无
</zho_l>
<chy_l>
夹衣类
</chy_l>
<ktk_l>
比较适宜(制热)
</ktk_l>
<xcz_l>
不太适宜
</xcz_l>
<diy_l>
暂无
</diy_l>
<pollution_s>
对空气污染物扩散无明显影响
</pollution_s>
<zwx_s>
紫外线弱
</zwx_s>
<ssd_s>
适宜在自然环境及露天场所活动。
</ssd_s>
<ktk_s>
比较适宜开启空调
</ktk_s>
<xcz_s>
洗车后未来1-2天内有降水、大风或沙尘天气,或洗车当日气温太低容易结冰。不太适宜洗车。
</xcz_s>
<gm>
1
</gm>
<gm_l>
低发期
</gm_l>
<gm_s>
天气舒适,不易发生感冒;
</gm_s>
<yd>
2
</yd>
<yd_l>
比较适宜
</yd_l>
<yd_s>
虽然天空云层较厚,比较适宜户外运动;
</yd_s>
<savedate_weather>
2011-12-14
</savedate_weather>
<savedate_life>
2011-12-14
</savedate_life>
<savedate_zhishu>
2011-12-14
</savedate_zhishu>
</Weather>
</Profiles>
解析类:继承DefaultHandler类
package wu.lis.action;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import wu.lis.model.Weather;
import wu.lis.util.Constant;
public class WeatherHandler extends DefaultHandler {
private Weather weather;
private String
preTag
=
null
;// 作用是记录解析时的上一个节点名称
@Override
public void startDocument() throws SAXException {
weather
=
new
Weather();
System.out.println("startDocument");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
preTag
=
qName
;
System.out.println(preTag);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String
result
=
new
String(ch, start, length);
if (preTag.equals(Constant.City)) {
if (weather.getCity() == null)
weather.setCity(result);
}
if (preTag.equals(Constant.Status1)) {
if (weather.getStatus1() == null)
weather.setStatus1(result);
}
if (preTag.equals(Constant.Status2)) {
if (weather.getStatus2() == null)
weather.setStatus2(result);
}
if (preTag.equals(Constant.Figure1)) {
if (weather.getFigure1() == null)
weather.setFigure1(result);
}
if (preTag.equals(Constant.Figure2)) {
if (weather.getFigure2() == null)
weather.setFigure2(result);
}
if (preTag.equals(Constant.Direction1)) {
if (weather.getDirection1() == null)
weather.setDirection1(result);
}
if (preTag.equals(Constant.Direction2)) {
if (weather.getDirection2() == null)
weather.setDirection2(result);
}
if (preTag.equals(Constant.Power1)) {
if (weather.getPower1() == null)
weather.setPower1(result);
}
if (preTag.equals(Constant.Power2)) {
if (weather.getPower2() == null)
weather.setPower2(result);
}
if (preTag.equals(Constant.Temperature1)) {
System.out.println(preTag);
if (weather.getTemperature1() == null)
weather.setTemperature1(result);
}
if (preTag.equals(Constant.Temperature2)) {
if (weather.getTemperature2() == null)
weather.setTemperature2(result);
}
if (preTag.equals(Constant.Ssd)) {
if (weather.getSsd() == null)
weather.setSsd(result);
}
if (preTag.equals(Constant.Ssd_l)) {
if (weather.getSsd_l() == null)
weather.setSsd_l(result);
}
if (preTag.equals(Constant.Ssd_s)) {
if (weather.getSsd_s() == null)
weather.setSsd_s(result);
}
if (preTag.equals(Constant.Tgd1)) {
if (weather.getTgd1() == null)
weather.setTgd1(result);
}
if (preTag.equals(Constant.Tgd2)) {
if (weather.getTgd2() == null)
weather.setTgd2(result);
}
if (preTag.equals(Constant.Zwx)) {
if (weather.getZwx() == null)
weather.setZwx(result);
}
if (preTag.equals(Constant.Zwx_l)) {
if (weather.getZwx_l() == null)
weather.setZwx_l(result);
}
if (preTag.equals(Constant.Zwx_s)) {
if (weather.getZwx_s() == null)
weather.setZwx_s(result);
}
if (preTag.equals(Constant.Ktk)) {
if (weather.getKtk() == null)
weather.setKtk(result);
}
if (preTag.equals(Constant.Ktk_l)) {
if (weather.getKtk_l() == null)
weather.setKtk_l(result);
}
if (preTag.equals(Constant.Ktk_s)) {
if (weather.getKtk_s() == null)
weather.setKtk_s(result);
}
if (preTag.equals(Constant.Pollution))
if (weather.getPollution() == null)
weather.setPollution(result);
if (preTag.equals(Constant.Pollution_l))
if (weather.getPollution_l() == null)
weather.setPollution_l(result);
if (preTag.equals(Constant.Pollution_s))
if (weather.getPollution_s() == null)
weather.setPollution_s(result);
if (preTag.equals(Constant.Xcz))
if (weather.getXcz() == null)
weather.setXcz(result);
if (preTag.equals(Constant.Xcz_l))
if (weather.getXcz_l() == null)
weather.setXcz_l(result);
if (preTag.equals(Constant.Xcz_s))
if (weather.getXcz_s() == null)
weather.setXcz_s(result);
if (preTag.equals(Constant.Chy))
if (weather.getChy() == null)
weather.setChy(result);
if (preTag.equals(Constant.Chy_l))
if (weather.getChy_l() == null)
weather.setChy_l(result);
if (preTag.equals(Constant.Chy_shuoming))
if (weather.getChy_shuoming() == null)
weather.setChy_shuoming(result);
if (preTag.equals(Constant.Gm))
if (weather.getGm() == null)
weather.setGm(result);
if (preTag.equals(Constant.Gm_l))
if (weather.getGm_l() == null)
weather.setGm_l(result);
if (preTag.equals(Constant.Gm_s))
if (weather.getGm_s() == null)
weather.setGm_s(result);
if (preTag.equals(Constant.Yd))
if (weather.getYd() == null)
weather.setYd(result);
if (preTag.equals(Constant.Yd_l))
if (weather.getYd_l() == null)
weather.setYd_l(result);
if (preTag.equals(Constant.Yd_s))
if (weather.getYd_s() == null)
weather.setYd_s(result);
if (preTag.equals(Constant.Savedate_weather))
if (weather.getSavedate_weather() == null)
weather.setSavedate_weather(result);
if (preTag.equals(Constant.Savedate_life))
if (weather.getSavedate_life() == null)
weather.setSavedate_life(result);
if (preTag.equals(Constant.Savedate_zhishu))
if (weather.getSavedate_zhishu() == null)
weather.setSavedate_zhishu(result);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("Profiles")) {
preTag
=
null
;
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("城市:" + weather.getCity());
System.out.println("白天:" + weather.getStatus1());
System.out.println("夜间:" + weather.getStatus2());
System.out.println("白天风向:"+weather.getDirection1());
System.out.println("夜间风向:"+weather.getDirection2());
System.out.println("白天温度:" + weather.getTemperature1());
System.out.println("夜间温度:" + weather.getTemperature2());
}
}
调用解析
InputStream
inStream
=
getClass
().getClassLoader().getResourceAsStream("weather.xml");
System.out.println(result);
SAXParserFactory
factory
=
SAXParserFactory
.newInstance();
try {
SAXParser
parser
=
factory
.newSAXParser();
XMLReader
reader
=
parser
.getXMLReader();
reader.setContentHandler(new WeatherHandler());
reader.parse(new InputSource(inStream));
} catch (Exception e) {
e.printStackTrace();
}
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图