Android SAX解析xml为java

主类:

package com.example.day12saxjiexi;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

//import okhttp3.Call;
//import okhttp3.Callback;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Response;
//import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button downLoad;
    private TextView showXMLcontent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        downLoad = (Button) findViewById(R.id.downLoad);
        showXMLcontent = (TextView) findViewById(R.id.showXMLcontent);

        downLoad.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.downLoad:

                okhttpjiexi();

                break;
        }
    }

    private void okhttpjiexi() {


//        OkHttpClient okHttpClient = new OkHttpClient();
//          个人服务器没开启,无法使用
//        final Request request = new Request.Builder().get().url("http://169.254.35.202:9527/1604A/app.xml").build();//请求获取到网络的资源
//        //回调的方法
//        okHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Toast.makeText(MainActivity.this, "coming", Toast.LENGTH_SHORT).show();
//
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                //response相当于获取到的资源
//                ResponseBody body = response.body();
//                byte[] bytes = body.bytes();//资源的byte类型
//                String result = new String(bytes);//获取资源string类型

        parseXMLWithSAX();

//            }
//        });

    }

    private void parseXMLWithSAX() {


        //SAX解析
        SAXParserFactory factory = SAXParserFactory.newInstance();//创建一个解析SAX工厂类
        try {
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();

            //主要的
            MySaxHanler handler = new MySaxHanler();
            xmlReader.setContentHandler(handler);

            //获取到资源类
            AssetManager assets = getAssets();
            InputStream is = assets.open("app.xml");

            //开始执行解析
            xmlReader.parse(new InputSource(is));


        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

MySaxHanler类:

package com.example.day12saxjiexi;

import android.util.Log;

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

//继承一个DefaultHander
public class MySaxHanler extends DefaultHandler {

    private String nodeName;
    private StringBuilder id;
    private StringBuilder name;
    private StringBuilder version;


    public MySaxHanler() {
        super();

    }

    //初始化
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();

        id = new StringBuilder();
        name = new StringBuilder();
        version = new StringBuilder();
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    //在开始解析某个节点的时候被掉用
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        nodeName = localName;

    }

    //在结束解析某个节点的时候被掉用
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);

        if ("app".equals(localName)) {
            Log.e("hh", String.valueOf(id));
            Log.e("hh", String.valueOf(name));
            Log.e("hh", String.valueOf(version));

            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }

    }

    //在获取到节点中的内容的时候被调用
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);

        if ("id".equals(nodeName)) {
            id.append(ch, start, length);
        } else if ("name".equals(nodeName)) {
            name.append(ch, start, length);
        } else if ("version".equals(nodeName)) {
            version.append(ch, start, length);
        }

    }
}

app.xml:



    

        1

        jim

        河北
    

    

        2

        anna

        北京
    
    

        3

        tom

        天津
    



添加权限:

你可能感兴趣的:(Android SAX解析xml为java)