<?xml version="1.0" encoding="utf-8"?> <channel> <item id="0" url="http://www.baidu.com">百度</item> <item id="1" url="http://www.qq.com">腾讯</item> <item id="2" url="http://www.sina.com.cn">新浪</item> <item id="3" url="http://www.taobao.com">淘宝</item> </channel>
public class SAXPraserHelper extends DefaultHandler {
final int ITEM = 0x0005;
List<channel> list;
channel chann;
int currentState = 0;
public List<channel> getList() {
return list;
}
/*
* 接口字符块通知
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
// super.characters(ch, start, length);
String theString = String.valueOf(ch, start, length);
if (currentState != 0) {
chann.setName(theString);
currentState = 0;
}
return;
}
/*
* 接收文档结束通知
*/
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
/*
* 接收标签结束通知
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("item"))
list.add(chann);
}
/*
* 文档开始通知
*/
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
list = new ArrayList<channel>();
}
/*
* 标签开始通知
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
chann = new channel();
if (localName.equals("item")) {
for (int i = 0; i < attributes.getLength(); i++) {
if (attributes.getLocalName(i).equals("id")) {
chann.setId(attributes.getValue(i));
} else if (attributes.getLocalName(i).equals("url")) {
chann.setUrl(attributes.getValue(i));
}
}
currentState = ITEM;
return;
}
currentState = 0;
return;
}
}
private List<channel> getChannelList() throws ParserConfigurationException, SAXException, IOException
{
//实例化一个SAXParserFactory对象
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser;
//实例化SAXParser对象,创建XMLReader对象,解析器
parser=factory.newSAXParser();
XMLReader xmlReader=parser.getXMLReader();
//实例化handler,事件处理器
SAXPraserHelper helperHandler=new SAXPraserHelper();
//解析器注册事件
xmlReader.setContentHandler(helperHandler);
//读取文件流
InputStream stream=getResources().openRawResource(R.raw.channels);
InputSource is=new InputSource(stream);
//解析文件
xmlReader.parse(is);
return helperHandler.getList();
}
private List<Map<String, String>> getData() {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
XmlResourceParser xrp = getResources().getXml(R.xml.channels);
try {
// 直到文档的结尾处
while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
// 如果遇到了开始标签
if (xrp.getEventType() == XmlResourceParser.START_TAG) {
String tagName = xrp.getName();// 获取标签的名字
if (tagName.equals("item")) {
Map<String, String> map = new HashMap<String, String>();
String id = xrp.getAttributeValue(null, "id");// 通过属性名来获取属性值
map.put("id", id);
String url = xrp.getAttributeValue(1);// 通过属性索引来获取属性值
map.put("url", url);
map.put("name", xrp.nextText());
list.add(map);
}
}
xrp.next();// 获取解析下一个事件
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static List<channel> getChannelList(InputStream stream)
{
List<channel> list=new ArrayList<channel>();
//得到 DocumentBuilderFactory 对象, 由该对象可以得到 DocumentBuilder 对象
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try {
//得到DocumentBuilder对象
DocumentBuilder builder=factory.newDocumentBuilder();
//得到代表整个xml的Document对象
Document document=builder.parse(stream);
//得到 "根节点"
Element root=document.getDocumentElement();
//获取根节点的所有items的节点
NodeList items=root.getElementsByTagName("item");
//遍历所有节点
for(int i=0;i<items.getLength();i++)
{
channel chann=new channel();
Element item=(Element)items.item(i);
chann.setId(item.getAttribute("id"));
chann.setUrl(item.getAttribute("url"));
chann.setName(item.getFirstChild().getNodeValue());
list.add(chann);
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有