SAX解析

来源:互联网 发布:经典网络流行语言大全 编辑:程序博客网 时间:2024/05/04 13:52
public class SAXHandler extends DefaultHandler {
 
    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);
            }
        });
    }
 
}






0 0
原创粉丝点击