SAX解析
来源:互联网 发布:经典网络流行语言大全 编辑:程序博客网 时间:2024/05/04 13:52
public static List<DataBean>list;
DataBean databean;
private String str;
String txt;
/*
* 文档开始解析
*/
@Override
public void startDocument() throws SAXException {
super.startDocument();
Log.e("TAG", "整个文档开始解析");
//创建集合
list=new ArrayList<DataBean>();
}
/*
* 标签开始
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
Log.e("TAG", "标签开始解析");
str=localName;
if(localName.equals("food")){
//实例化封装类
databean=new DataBean();
//将解析的数据放入封装类中
databean.setId(attributes.getValue("id"));
// String iamg=attributes.getValue("imageurl");
// databean.setImageurl(Integer.parseInt(iamg));
databean.setImageurl(attributes.getValue("imageurl"));
databean.setFoodtitle(attributes.getValue("foodtitle"));
databean.setFoodcontent(attributes.getValue("foodcontent"));
databean.setFoodprice(attributes.getValue("foodprice"));
Log.e("TAG", "属性==="+attributes.getValue("id")+",,"
+attributes.getValue("imageurl")+",,"
+attributes.getValue("foodtitle")+",,"+attributes.getValue("foodcontent")+",,"+attributes.getValue("foodprice"));
}
}
/*
* 文本开始
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
txt=new String(ch, start, length);
}
/*
* 标签结束
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
Log.e("TAG", "标签解析结束");
if(localName.equals("imageurl")){
//databean.setImageurl(Integer.parseInt(txt));
databean.setImageurl(txt);
Log.e("TAG", "imageurl="+txt);
}else if(localName.equals("foodtitle")){
databean.setFoodtitle(txt);
Log.e("TAG", "foodtitle="+txt);
}else if(localName.equals("foodcontent")){
databean.setFoodcontent(txt);
Log.e("TAG", "foodcontent="+txt);
}else if(localName.equals("foodprice")){
databean.setFoodprice(txt);
Log.e("TAG", "foodprice="+txt);
}else if(localName.equals("food")){
//把对象加入集合
list.add(databean);
}
str="";
}
/*
* 文档结束
*/
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
Log.e("TAG", "文档结束");
}
public List<DataBean>getlist(){
return list;
}
}
public class MainActivity extends Activity {
private Button bt_jiexi;
private Button bt_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// 找对象
bt_jiexi=(Button)findViewById(R.id.bt_jiexi);
bt_show=(Button)findViewById(R.id.bt_show);
bt_jiexi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//实例化帮助类
SAXHandler saxhandler=new SAXHandler();
try {
//创建SAX解析器
SAXParser parser=factory.newSAXParser();
//获得要解析的文件
try {
parser.parse(getAssets().open("data.xml"),saxhandler);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<DataBean> list=saxhandler.getlist();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
bt_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到展示页面
Intent intent=new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent);
}
});
}
}
- SAX解析
- SAX解析
- SAX解析
- SAX解析
- SAX解析
- SAX解析
- SAX解析
- SAX解析
- SAX 解析
- sax解析
- SAX解析
- SAX解析
- SAX解析
- sax解析
- SAX解析
- SAX解析
- SAX解析
- sax解析
- 第四部视频,CC2538TinyOS之CoAP协议无线点灯例程+firefox网页访问(含CC2530 COAP)
- QT学习 之 事件与事件过滤器
- c语言学习(while)中自己遇到的陷阱
- php学习笔记八:数据类型
- android下运行时动态链接dlopen()和dlsym()的实现
- SAX解析
- php学习笔记九:Array数组
- 远程通信之文件共享
- c++ 保留id排序
- hdoj 1048 The Hardest Problem Ever【基础】
- php学习笔记十:类与对象-基础
- Android ANR 分析
- UIControlEventEditingDidEnd和UIControlEventEditingDidOnExit各自的触发时机
- HBase过滤器