android解析XML总结(SAX、Pull、Dom三种方式)

源地址:http://www.cnblogs.com/JerryWang1991/archive/2012/02/24/2365507.html

关于SAX的使用体会:

package com.example.li.demoforxml.XMLHelper;

import android.util.Log;

import com.example.li.demoforxml.Entity.channel;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by li on 2016/3/15.
 */
public class SAXPraserHelper extends DefaultHandler{

    final static private String TAG="Demo";
    int currentState =0;
    channel chann;
    List<channel> list;

    public List<channel> getList() {
        return list;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        Log.v(TAG,"charactersbegin");
        String theString = String.valueOf(ch,start,length);
        Log.v(TAG,"--->theString-->"+theString+"--->"+currentState+"<---");
        if(currentState != 0){
            Log.v(TAG,"set"+theString);
            chann.setName(theString);
            currentState = 0;
        }
        Log.v(TAG,"characterend");
        return;
    }

    /**
     *文档解析开始
     */
    @Override
    public void startDocument() throws SAXException {
        Log.v(TAG,"startDocumentbegin");
        list = new ArrayList<channel>();
        Log.v(TAG,"startDocumentend");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        Log.v(TAG,"starElementbegin");
        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 = 1;
            Log.v(TAG,"-->"+currentState);
            Log.v(TAG,"startElementend---if");
            //return用来终止,防止currentState重新又变成0
            return;
        }
        Log.v(TAG,"startElementend");
        currentState = 0;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        Log.v(TAG, "endElementstart");
        if(localName.equals("item")) {
            list.add(chann);
            Log.v(TAG, "endElementend---if");
        }
        Log.v(TAG,"endElementend");
    }

    /**
     * 文档解析结束
     * @throws SAXException
     */
    @Override
    public void endDocument() throws SAXException {
        Log.v(TAG,"endDocumentstart");
        super.endDocument();
        Log.v(TAG, "endDocumentend");
    }
}
03-15 23:42:15.200 4588-4588/com.example.li.demoforxml V/Demo: startDocumentbegin
03-15 23:42:15.200 4588-4588/com.example.li.demoforxml V/Demo: startDocumentend
03-15 23:42:15.201 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.205 4588-4588/com.example.li.demoforxml V/Demo: startElementend
03-15 23:42:15.205 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->    --->0<---
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->百度--->1<---
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: set百度
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->    --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->腾讯--->1<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: set腾讯
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->    --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->新浪--->1<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: set新浪
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->    --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->淘宝--->1<---
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: set淘宝
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endDocumentstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endDocumentend
日志文件显示除了最后一个循环,都是调用了两次characterend,不懂这个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值