解析XML

在tomcat的webapps下ROOT目录下创建xml文件,记得右键另存改编码方式为UTF-8

下面上代码:

xml部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取网络XML数据"
        android:onClick="getXMLByNet"
        />

</LinearLayout>

java部分:

package com.example.prase;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void getXMLByNet(View view){
        new MyTask().execute();
    }
    class MyTask extends AsyncTask{
        @Override
        protected Object doInBackground(Object[] params) {
            //获取网络XML数据
            String path="http://192.168.23.4:7788/students.xml";
            try {
                URL url=new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //设置请求方式
                connection.setRequestMethod("GET");
                //设置请求超时的时间
                connection.setConnectTimeout(5000);
                //获取结果码
                int code=connection.getResponseCode();
                if(code==200){
                    //获取数据
                    InputStream is=connection.getInputStream();
                    int len=0;
                    byte[] buf=new  byte[1024];
                    StringBuffer stringBuffer=new StringBuffer();
                    while((len=is.read(buf))!=-1){
                        String s=new String(buf,0,len);
                        stringBuffer.append(s);
                    }
                    Log.i("test",stringBuffer.toString());

                    //解析xml
                   XmlPullParser pullParser= Xml.newPullParser();
                    try {
                        pullParser.setInput(is,"UTF-8");
                        int type=pullParser.getEventType();
                        while(type!=XmlPullParser.END_DOCUMENT){
                            switch (type) {
                                case XmlPullParser.START_DOCUMENT:
                                    //获取开始标签的名字
                                    String startTagName=pullParser.getName();
                                    if("student".equals(startTagName)){
                                        //获取属性ID的值
                                        String sid = pullParser.getAttributeValue(0);
                                    }else if("sname".equals(startTagName)){
                                        String sname=pullParser.nextText();
                                    }else if("sage".equals(startTagName)){
                                        String sage=pullParser.nextText();
                                    }
                                    break;
                                case XmlPullParser.END_TAG:
                                    break;
                            }
                            //细节:
                            type=pullParser.next();
                        }
                    } catch (XmlPullParserException e) {
                        e.printStackTrace();
                    }

                    is.close();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
        }
    }
}

还需要获取网络权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.prase">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值