xml的解析方式

1:pull解析

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url("http://192.168.43.24:11403/sss/he.xml")
                                .build();
                        try {
                            Response response = okHttpClient.newCall(request).execute();
                            String responsedata = response.body().string();
                            pullParseXML(responsedata);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                ).start();
            }
        });
    }

    private void pullParseXML(String responsedata) throws Exception {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlpullparser = factory.newPullParser();
        xmlpullparser.setInput(new StringReader(responsedata));
        int eventtype = xmlpullparser.getEventType();
        String id = "";
        String name = "";
        String version = "";
        while (eventtype != XmlPullParser.END_DOCUMENT) {
            Log.i("xinxi","eventtype:"+eventtype);
            String nodename = xmlpullparser.getName();
            Log.i("xinxi","nodename:"+nodename);
            switch (eventtype) {
                case XmlPullParser.START_TAG: {
                    if ("id".equals(nodename)) {
                        id = xmlpullparser.nextText();
                    } else if ("name".equals(nodename)) {
                        name = xmlpullparser.nextText();
                    } else if ("version".equals(nodename)) {
                        version = xmlpullparser.nextText();
                    }
                    break;
                }
                case XmlPullParser.END_TAG: {
                    if ("app".equals(nodename)) {
                        Log.i("xinxi", "id is:" + id);
                        Log.i("xinxi", "name is:" + name);
                        Log.i("xinxi", "version is:" + version);
                    }
                    break;
                }
                default:
                    break;
            }
            eventtype = xmlpullparser.next();
        }
    }
}
/*
在tomcat上原数据是



1
google
1.0


2
baidu
2.0


3
souhu
3.0


经过解析,输出结果为:
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:0
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:apps
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:id
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:name
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:version
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:3
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: id is:1
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: name is:google
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: version is:1.0
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:id
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:name
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:version
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:3
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: id is:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: name is:baidu
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: version is:2.0
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:id
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:name
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:2
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:version
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:3
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:app
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: id is:3
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: name is:souhu
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: version is:3.0
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:4
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:null
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: eventtype:3
11-16 16:03:02.796 1458-1520/com.example.liang.myapplication I/xinxi: nodename:apps*/

2:sax解析

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        Request request = new Request.Builder()
                                .url("http://192.168.43.24:11403/sss/he.xml")
                                .build();
                        try {
                            Response response = okHttpClient.newCall(request).execute();
                            String responsedata = response.body().string();
                            saxParseXML(responsedata);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                ).start();
            }
        });
    }

    private void saxParseXML(String responsedata) throws Exception {
        SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
        XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader();
        wodehandler wodehandler=new wodehandler();
        xmlReader.setContentHandler(wodehandler);
        xmlReader.parse(new InputSource(new StringReader(responsedata)));
    }
}
/*tomcat上的数据为:



1
google
1.0


2
baidu
2.0


3
souhu
3.0


经过解析,输出结果为:
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: id is:1
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: name is:google
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: version is:1.0
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: id is:2
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: name is:baidu
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: version is:2.0
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: id is:3
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: name is:souhu
        11-16 17:04:22.216 14368-14688/com.example.liang.myapplication I/xinxi: version is:3.0*/

在wodehandler.java文件中

import android.util.Log;

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

/**
 * Created by liang on 2017/11/16.
 */

public class wodehandler extends DefaultHandler {
    private String nodeName;
    private StringBuilder id;//StringBuffer和StringBuilder区别在于StringBufferd支持并发操作,线性安全的,适 合多线程中使用。
    // StringBuilder不支持并发操作,线性不安全的,不适合多线程中使用。新引入的StringBuilder类不是
    // 线程安全的,但其在单线程中的性能比StringBuffer高
    private StringBuilder name;
    private StringBuilder version;
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        id=new StringBuilder();
        name=new StringBuilder();
        version=new StringBuilder();
    }

    @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 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);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if("app".equals(localName)){
            Log.i("xinxi", "id is:" + id.toString().trim());
            Log.i("xinxi", "name is:" + name.toString().trim());
            Log.i("xinxi", "version is:" + version.toString().trim());
            id.setLength(0);
            name.setLength(0);
            version.setLength(0);
        }
    }

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

3:dom解析

请参考https://blog.csdn.net/xiaanming/article/details/8942429

你可能感兴趣的:(xml的解析方式)