config.xml文件如下:
用到的几个主要的类:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
XmlParser类:主要用于处理读取到的数据,如标签,文本等信息。
public ArrayList<String> getArrayString(){
return array;
}
//读取到开始标签信息时回调
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
if(localName.equals("string-array")){
mName = attributes.getValue(0);
if(mName.equals("system_app_ti60")){
mFound = true;
}
}
}
//读取到文本信息时回调
@Override
public void characters(char ch [ ], int start, int length) throws SAXException{
if(mFound){
String value = new String(ch,start,length);
if(!value.endsWith("apk"))
return;
array.add(value);
}
}
//读取到结束标签信息时回调
@Override
public void endElement(String uri, String localName, String qName) throws SAXException{
if(localName.equals("string-array")){
if(mFound){
mFound = false;
}
}
}
}
调用测试:
}
catch(SAXException e){
Log.i("hxd","SAXException");
return;
}
catch(ParserConfigurationException e){
Log.i("hxd","ParserConfigurationException");
return;
}
catch(IOException e){
Log.i("hxd","IOException");
return;
}
}