SAX解析XML是通过逐行扫描文件,他与DOM相比的优势是耗内存小,解析速度快,劣势是流式扫描,不会记录上下节点的关系,遇到符合的就扫描,其节点关系不得而知。
ContentHandler解析器使用方法,常用方法为startDocument、endDocument、startElement、endElement、characters
1.startElement方法说明
- void startElement(String uri,
- String localName,
- String qName,
- Attributes atts)
- throws SAXException
- 方法说明:
- 解析器在 XML 文档中的每个元素的开始调用此方法;对于每个 startElement 事件都将有相应的 endElement 事件(即使该元素为空时)。所有元素的内容都将在相应的 endElement 事件之前顺序地报告。
- 参数说明:
- uri - 名称空间 URI,如果元素没有名称空间 URI,或者未执行名称空间处理,则为空字符串
- localName - 本地名称(不带前缀),如果未执行名称空间处理,则为空字符串
- qName - 限定名(带有前缀),如果限定名不可用,则为空字符串
- atts - 连接到元素上的属性。如果没有属性,则它将是空 Attributes 对象。在 startElement 返回后,此对象的值是未定义的
- void endElement(String uri,
- String localName,
- String qName)
- throws SAXException接收元素结束的通知。
- SAX 解析器会在 XML 文档中每个元素的末尾调用此方法;对于每个 endElement 事件都将有相应的 startElement 事件(即使该元素为空时)。
- 参数:
- uri - 名称空间 URI,如果元素没有名称空间 URI,或者未执行名称空间处理,则为空字符串
- localName - 本地名称(不带前缀),如果未执行名称空间处理,则为空字符串
- qName - 限定的 XML 名称(带前缀),如果限定名不可用,则为空字符串
3.characters方法
- void characters(char[] ch,
- int start,
- int length)
- throws SAXException
- 接收字符数据的通知,可以通过new String(ch,start,length)构造器,创建解析出来的字符串文本.
- 参数:
- ch - 来自 XML 文档的字符
- start - 数组中的开始位置
- length - 从数组中读取的字符的个数
通过SAXParserFactory、SAXParser、XMLReader完成
XML文件:city.xml
<China>
<province id="34" name="台湾">
<city id="3401" name="台北">
<county id="340101" name="台北" weatherCode="101340101"/>
<county id="340102" name="桃园" weatherCode="101340102"/>
<county id="340103" name="新竹" weatherCode="101340103"/>
<county id="340104" name="宜兰" weatherCode="101340104"/>
</city>
<city id="3402" name="高雄">
<county id="340201" name="高雄" weatherCode="101340201"/>
<county id="340202" name="嘉义" weatherCode="101340202"/>
<county id="340203" name="台南" weatherCode="101340203"/>
<county id="340204" name="台东" weatherCode="101340204"/>
<county id="340205" name="屏东" weatherCode="101340205"/>
</city>
<city id="3403" name="台中">
<county id="340301" name="台中" weatherCode="101340401"/>
<county id="340302" name="苗栗" weatherCode="101340402"/>
<county id="340303" name="彰化" weatherCode="101340403"/>
<county id="340304" name="南投" weatherCode="101340404"/>
<county id="340305" name="花莲" weatherCode="101340405"/>
<county id="340306" name="云林" weatherCode="101340406"/>
</city>
</province>
</China>
获得InputStream
InputStream inputStream = getAssets().open("city.xml");
List<ProvicneModel> provicneModelList = CityParseHandler.getProvince(inputStream);
CityParseHandler解析类
public class CityParseHandler extends BaseParseHandler {
public static List<ProvicneModel> provicneModelList;
public List<AreaModel> areaModelList;
public WeatherModel weatherModel;
private ProvicneModel provicneModel;
private AreaModel areaModel;
private CityMode cityMode;
public String name;
public static List<ProvicneModel> getProvince(InputStream inputStream) throws Exception{
//得到SAX解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//创建解析器
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
//得到输入流
InputSource inputSource = new InputSource(inputStream);
CityParseHandler cityParseHandler = new CityParseHandler();
xmlReader.setContentHandler(new CityParseHandler());
xmlReader.parse(inputSource);
return CityParseHandler.provicneModelList;
}
@Override
public void startDocument() throws SAXException {
super.startDocument(); //To change body of overridden methods use File | Settings | File Templates.
provicneModelList = new ArrayList<ProvicneModel>();
Log.i(TAG, "startDocument") ;
}
@Override
public void endDocument() throws SAXException {
super.endDocument(); //To change body of overridden methods use File | Settings | File Templates.
Log.i(TAG, "endDocument") ;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes); //To change body of overridden methods use File | Settings | File Templates.
//省
if(localName.equals("province")){
System.out.println("localName" + localName);
provicneModel = new ProvicneModel();
provicneModel.setCityName(attributes.getValue("name"));
provicneModel.setCityId(attributes.getValue("id"));
}
if(localName.equals("city")){
cityMode = new CityMode();
cityMode.setCityName(attributes.getValue("name"));
cityMode.setCityId(attributes.getValue("id"));
}
if(localName.equals("county")){
areaModel = new AreaModel();
areaModel.setCityName("name");
areaModel.setCityId("id");
areaModel.setWeatherCode("weatherCode");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName); //To change body of overridden methods use File | Settings | File Templates.
if(localName.equals("province")){
provicneModelList.add(provicneModel);
provicneModel = null;
}
if(localName.equals("city")){
provicneModel.getCityModeList().add(cityMode);
cityMode= null;
}
if(localName.equals("county")){
cityMode.getAreaModelList().add(areaModel);
areaModel = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length); //To change body of overridden methods use File | Settings | File Templates.
}
}