解析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>
还没有评论,来说两句吧...