Android analysis of Xml format data

Parse Xml format data

1. Build a simplest web server, provide a piece of xml text on this server, then we visit this server in the program, and then parse the obtained XML text.
Download an Apache server installation package and open the service after installation. You can open the computer's browser to verify it. Enter 127.0.0.1 in the address bar. If the following figure appears, the server has been started successfully.
Write picture description here
Next, go to the Apache\htdocs directory under the directory where you installed Apache, and create a new file named get_data.xml here, then edit this file and add the content in XML format:
Write picture description here

	<apps>
		<app>
			<id>1</id>
			<name>Google Maps</name>
			<version>1.0</version>
		</app>
		<app>
			<id>2</id>
			<name>Chome</name>
			<version>2.1</version>
		</app>
		<app>
			<id>3</id>
			<name>Google Play</name>
			<version>2.3</version>
		</app>
	</apps>

Access in the browser will appear as shown in the figure:

Ok, the preparation work is over, let's get this XML data in the Android program.

Pull analysis method:

There are actually many ways to parse data in xml format. In this section, we will learn two commonly used methods, Pull analysis and SAX analysis. For the sake of simplicity, we continue to develop on the basis of the NetworkTest project, so that we can reuse the code of the previous network communication part, so that we can focus on XML data parsing.
Now that the data in XML format has been provided, what we have to do now is to parse out the part of the content we want to get.
Modify the code in MainActivity as follows:

	package net.nyist.lenovo.networktest;
	
	
	import android.support.v7.app.AppCompatActivity;
	import android.os.Bundle;
	import android.util.Log;
	import android.view.View;
	import android.widget.Button;
	import android.widget.TextView;
	
	import org.xmlpull.v1.XmlPullParser;
	import org.xmlpull.v1.XmlPullParserException;
	import org.xmlpull.v1.XmlPullParserFactory;
	
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.InputStreamReader;
	import java.io.StringReader;
	import java.net.HttpURLConnection;
	import java.net.MalformedURLException;
	import java.net.URL;
	
	import okhttp3.OkHttpClient;
	import okhttp3.Request;
	import okhttp3.Response;
	
	public class MainActivity extends AppCompatActivity implements View.OnClickListener{
	
	    TextView responseText;
	
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        Button sendRequest = (Button) findViewById(R.id.send_request);
	        responseText = (TextView) findViewById(R.id.response_text);
	        sendRequest.setOnClickListener(this);
	    }
	
	    @Override
	    public void onClick(View v) {
	            if (v.getId()==R.id.send_request){
	                //sendRequestWithHttpURLConnection();
	                sendRequestWithOkHttp();
	            }
	    }
	
	    private void sendRequestWithOkHttp() {
	        new Thread(new Runnable() {
	            @Override
	            public void run() {
	                OkHttpClient client = new OkHttpClient();
	                Request request = new Request.Builder()
	                        //指定访问的服务器地址是电脑本机
	                        .url("http://10.0.2.2/get_data.xml")
	                        .build();
	                try {
	                    Response response = client.newCall(request).execute();
	                    String responseData = response.body().string();
	                    parseXMLWithPull(responseData);
	                    //showResponse(responseData);
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }).start();
	    }
	    private void parseXMLWithPull(String xmlData) {
	        try {
	            //首先获取一个XmlPullParserFactory的实例,主要调用其静态方法newInstance()得到。
	            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
	            //然后利用XmlPullParserFactory的实例,调用newPullParser()方法,得到XmlPullParser对象。
	            XmlPullParser xmlPullParser = factory.newPullParser();
	            //然后调用XmlPullParser的setInput()方法将服务器返回的XML数据设置进去就可以开始解析了。
	            // 注意:这里的StringReader类是Reader类的子类。
	            //public class StringReaderextends ReaderAspecialized Reader that reads characters from a String in a sequential manner.
	            xmlPullParser.setInput(new StringReader(xmlData));
	            //具体解析的时候,首先通过XmlPullParser类的getEventType()方法得到当前的解析事件。
	            int eventType = xmlPullParser.getEventType();
	            String id = "";
	            String name = "";
	            String version = "";
	            //然后在一个while循环中不断地进行解析,
	            // 如果当前的解析事件不等于XmlPullParser.END_DOCUMENT,
	            // 说明解析工作还没完成,调用XmlPullParser的next()方法可以获取下一个解析事件。
	            while(eventType != XmlPullParser.END_DOCUMENT){
	                String nodeName = xmlPullParser.getName();
	                switch (eventType){
	                    //开始解析某个节点
	                    /*
	                    * 在while循环中,通过XmlPullParser类的getName()方法得到当前结点的名字,
	                    * 如果发现结点名等于id,name,或version,
	                    * 就调用nextText()方法来获取结点内具体的内容
	                    * ,每当解析完一个app结点后就将获取到的内容打印出来。
	                    * */
	                    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.d("MainActivity","id is"+id);
	                            Log.d("MainActivity","name is"+name);
	                            Log.d("MainActivity","version is"+version);
	                        }
	                        break;
	                    }
	                    default:
	                        break;
	                }
	                eventType = xmlPullParser.next();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	
	    private void sendRequestWithHttpURLConnection() {
	        //开启线程来发起网络请求
	        new Thread(new Runnable() {
	            @Override
	            public void run() {
	                HttpURLConnection connection = null;
	                BufferedReader reader = null;
	                try {
	                    URL url = new URL("https://www.baidu.com");
	                    connection =(HttpURLConnection) url.openConnection();
	                    connection.setRequestMethod("GET");
	                    connection.setConnectTimeout(8000);
	                    connection.setReadTimeout(8000);
	                    InputStream in = connection.getInputStream();
	                    //下面对获取到的输入流进行读取
	                    reader = new BufferedReader(new InputStreamReader(in));
	                    StringBuilder response = new StringBuilder();
	                    String line;
	                    while ((line = reader.readLine())!=null){
	                        response.append(line);
	                    }
	                    showResponse(response.toString());
	                } catch (Exception e) {
	                    e.printStackTrace();
	                }finally {
	                    if (reader != null){
	                        try {
	                            reader.close();
	                        } catch (IOException e) {
	                            e.printStackTrace();
	                        }
	                    }
	                    if (connection!=null){
	                        connection.disconnect();
	                    }
	                }
	            }
	        }).start();
	    }
	
	    private void showResponse(final String response) {
	        runOnUiThread(new Runnable() {
	            @Override
	            public void run() {
	                //在这里进行UI操作,将结果显示到界面上
	                responseText.setText(response);
	            }
	        });
	    }
	}

Guess you like

Origin blog.csdn.net/i_nclude/article/details/77970744