ParseXML 和 parseJson

来源:互联网 发布:摇色子软件 编辑:程序博客网 时间:2024/06/08 10:20

1.解析网络数据:在tomcat的root下,分别写xml文件,json文件。注意编码格式,改为UTF-8;

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <student>  
  3.     <stu sid="1">  
  4.         <sname>1</sname>  
  5.         <sage>19</sage>  
  6.         <ssex></ssex>  
  7.     </stu>  
  8.     <stu sid="2">  
  9.         <sname>2</sname>  
  10.         <sage>19</sage>  
  11.         <ssex></ssex>  
  12.     </stu>  
  13.     <stu sid="3">  
  14.         <sname>3</sname>  
  15.         <sage>19</sage>  
  16.         <ssex></ssex>  
  17.     </stu>  
  18. </student>  
json:

[html] view plain copy
 print?
  1. {"count":3,"students":[{"sid":1,"sname":"1"},{"sid":2,"sname":"2"},{"sid":3,"sname":"3"}]}  



[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     tools:context="com.zking.parsexml.MainActivity">  
  9.   
  10.    <Button  
  11.        android:layout_width="wrap_content"  
  12.        android:layout_height="wrap_content"  
  13.        android:text="获取xml的数据"  
  14.        android:onClick="getXML"/>  
  15.   
  16.   
  17.    <Button  
  18.        android:layout_width="wrap_content"  
  19.        android:layout_height="wrap_content"  
  20.        android:text="获取json的数据"  
  21.        android:onClick="getJson"/>  
  22.   
  23.   
  24. </LinearLayout>  

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.           package="com.zking.parsexml">  
  4.   
  5.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  6.   
  7.     <application  
  8.         android:allowBackup="true"  
  9.         android:icon="@mipmap/ic_launcher"  
  10.         android:label="@string/app_name"  
  11.         android:roundIcon="@mipmap/ic_launcher_round"  
  12.         android:supportsRtl="true"  
  13.         android:theme="@style/AppTheme">  
  14.         <activity android:name=".MainActivity">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN"/>  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER"/>  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>  

[html] view plain copy
 print?
  1. package com.zking.parsexml;  
  2.   
  3. import java.util.List;  
  4.   
  5. /**  
  6.  * Created by Administrator on 2017/7/26 0026.  
  7.  */  
  8.   
  9. public class Out {  
  10.     private int count;  
  11.     private List<Students> students;  
  12.   
  13.     public Out() {  
  14.     }  
  15.   
  16.     public Out(int count, List<Students> students) {  
  17.         this.count = count;  
  18.         this.students = students;  
  19.     }  
  20.   
  21.     public int getCount() {  
  22.         return count;  
  23.     }  
  24.   
  25.     public void setCount(int count) {  
  26.         this.count = count;  
  27.     }  
  28.   
  29.     public List<Students> getStudents() {  
  30.         return students;  
  31.     }  
  32.   
  33.     public void setStudents(List<Students> students) {  
  34.         this.students = students;  
  35.     }  
  36. }  

[html] view plain copy
 print?
  1. package com.zking.parsexml;  
  2.   
  3. /**  
  4.  * Created by Administrator on 2017/7/26 0026.  
  5.  */  
  6.   
  7. public class Students {  
  8.     private int sid;  
  9.     private String sname;  
  10.   
  11.     public Students() {  
  12.     }  
  13.   
  14.     public Students(int sid, String sname) {  
  15.         this.sid = sid;  
  16.         this.sname = sname;  
  17.     }  
  18.   
  19.     public int getSid() {  
  20.         return sid;  
  21.     }  
  22.   
  23.     public void setSid(int sid) {  
  24.         this.sid = sid;  
  25.     }  
  26.   
  27.     public String getSname() {  
  28.         return sname;  
  29.     }  
  30.   
  31.     public void setSname(String sname) {  
  32.         this.sname = sname;  
  33.     }  
  34. }  

[html] view plain copy
 print?
  1. package com.zking.parsexml;  
  2.   
  3. import android.app.ProgressDialog;  
  4. import android.os.AsyncTask;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.util.Xml;  
  9. import android.view.View;  
  10.   
  11. import com.alibaba.fastjson.JSON;  
  12.   
  13. import org.xmlpull.v1.XmlPullParser;  
  14. import org.xmlpull.v1.XmlPullParserException;  
  15.   
  16. import java.io.IOException;  
  17. import java.io.InputStream;  
  18. import java.net.HttpURLConnection;  
  19. import java.net.MalformedURLException;  
  20. import java.net.URL;  
  21. import java.util.List;  
  22.   
  23. public class MainActivity extends AppCompatActivity {  
  24.   
  25.     private ProgressDialog pd;  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.   
  32.         pd = new ProgressDialog(this);  
  33.         pd.setMessage("拼命加载中");  
  34.     }  
  35.   
  36.     public void getXML(View view){  
  37.         new MyGetXML().execute();  
  38.     }  
  39.   
  40.     class MyGetXML extends AsyncTask{  
  41.   
  42.         @Override  
  43.         protected Object doInBackground(Object[] params) {  
  44.             //在后台运行数据  
  45.             //获取访问网络数据路径  
  46.             String path="http://192.168.43.22:8080/student.xml";  
  47.             //加载路径  
  48.             try {  
  49.                 URL url=new URL(path);  
  50.                 //打开网址  
  51.                 HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();  
  52.                 //设置请求方式  
  53.                 httpURLConnection.setRequestMethod("GET");  
  54.                 //设置请求时间(获取数据)  
  55.                 httpURLConnection.setConnectTimeout(5000);  
  56.                 //获取响应码  
  57.                 int a=httpURLConnection.getResponseCode();  
  58.                 if(a==200){  
  59.                     //获取数据--以流的方式  
  60.                     InputStream is = httpURLConnection.getInputStream();  
  61.                    /* int len=0;  
  62.                     byte[]b=new byte[1024];  
  63.                     StringBuffer sb=new StringBuffer();  
  64.                     while ((lenis.read(b))!=-1){  
  65.                         String s=new String(b,0,len);  
  66.                         sb.append(s);  
  67.                     }*/  
  68.                     //解析XML  
  69.                     XmlPullParser xmlPullParserXml.newPullParser();  
  70.                     xmlPullParser.setInput(is,"UTF-8");  
  71.                     //解析标签类型  
  72.                     int type=xmlPullParser.getEventType();  
  73.                     while(type!=XmlPullParser.END_DOCUMENT){  
  74.                         switch (type) {  
  75.                             case XmlPullParser.START_TAG:  
  76.                                 //获取开始标签  
  77.                                 String start=xmlPullParser.getName();  
  78.                                 if("stu".equals(start)){  
  79.                                     String sidxmlPullParser.getAttributeValue(0);  
  80.                                     Log.i("test",sid);  
  81.                                 }else if("sname".equals(start)){  
  82.                                   String snamexmlPullParser.nextText();  
  83.                                     Log.i("test",sname);  
  84.                                 }else if("sage".equals(start)){  
  85.                                    String sagexmlPullParser.nextText();  
  86.                                     Log.i("test",sage);  
  87.                                 }else if("ssex".equals(start)){  
  88.                                     String ssex=xmlPullParser.nextText();  
  89.                                     Log.i("test",ssex);  
  90.                                 }  
  91.                                 break;  
  92.                             case XmlPullParser.END_TAG:  
  93.                                 break;  
  94.                         }  
  95.                         type=xmlPullParser.next();  
  96.                     }  
  97.                    // Log.i("test",sb.toString());  
  98.                     is.close();  
  99.                 }  
  100.   
  101.             } catch (MalformedURLException e) {  
  102.                 e.printStackTrace();  
  103.             } catch (IOException e) {  
  104.                 e.printStackTrace();  
  105.             } catch (XmlPullParserException e) {  
  106.                 e.printStackTrace();  
  107.             }  
  108.             return null;  
  109.         }  
  110.   
  111.         @Override  
  112.         protected void onPostExecute(Object o) {  
  113.             //刷新界面  
  114.             super.onPostExecute(o);  
  115.         }  
  116.     }  
  117.   
  118.   
  119.     public void getJson(View view){  
  120.         new MyGetJson().execute();  
  121.     }  
  122.   
  123.     class  MyGetJson extends AsyncTask{  
  124.         @Override  
  125.         protected void onPreExecute() {  
  126.             super.onPreExecute();  
  127.             pd.show();  
  128.   
  129.         }  
  130.   
  131.         @Override  
  132.         protected Object doInBackground(Object[] params) {  
  133.             //获取网络路径  
  134.             String path="http://192.168.43.22:8080/students.json";  
  135.             //加载网络路径  
  136.             try {  
  137.                 URL url=new URL(path);  
  138.                 HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();  
  139.                 //设置请求方式  
  140.                 httpURLConnection.setRequestMethod("GET");  
  141.                 //设置加载数据的时间  
  142.                 httpURLConnection.setConnectTimeout(5000);  
  143.                 if(httpURLConnection.getResponseCode()==200){  
  144.                     InputStream is=httpURLConnection.getInputStream();  
  145.                     int len=0;  
  146.                     byte[]b=new byte[1024];  
  147.                     StringBuffer sb=new StringBuffer();  
  148.                     while((len=is.read(b))!=-1){  
  149.                         String s=new String(b,0,len);  
  150.                         sb.append(s);  
  151.                     }  
  152.                     Log.i("test",sb.toString());  
  153.                     //解析json  
  154.                     Out out=JSON.parseObject(sb.toString(),Out.class);  
  155.                     int count=out.getCount();  
  156.                     List<Students> students=out.getStudents();  
  157.                     for (Students s : students) {  
  158.                         Log.i("test",s.getSid()+"  "+s.getSname());  
  159.                     }  
  160.   
  161.                     is.close();  
  162.   
  163.                 }  
  164.   
  165.   
  166.             } catch (MalformedURLException e) {  
  167.                 e.printStackTrace();  
  168.             } catch (IOException e) {  
  169.                 e.printStackTrace();  
  170.             }  
  171.   
  172.             return null;  
  173.         }  
  174.   
  175.         @Override  
  176.         protected void onPostExecute(Object o) {  
  177.             super.onPostExecute(o);  
  178.             pd.cancel();  
  179.         }  
  180.     }  
  181.   
  182.   
  183.   
  184.   
  185. }  
原创粉丝点击