xml作为客户端和服务器端进行数据交换的载体 ,以下是一个解析本地传过来的xml文件的例子。
部署一个web项目后,在项目中放置一个xml文件gril.xml, 内容为:
<?xml version="1.0" encoding="utf-8"?> <girls> <girl> <name>张三</name> <age>18</age> <school>北大</school> </girl> <girl> <name>李四</name> <age>20</age> <school>清华</school> </girl> </girls>
将web程序发布到你本地的服务器上。
使用Android studio创建一个Android项目。
创建类Gril.java
public class Gril { private String name; private int age; private String school; @Override public String toString() { // TODO Auto-generated method stub return "[name="+name+" age="+age+" school="+school+"]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
创建XMLThread.java类,代码如下:
import android.os.Handler; import android.util.Log; import android.widget.TextView; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * Created by pss on 2016/1/9. */ public class XMLThread extends Thread { private String url; private TextView textView; private Handler handler; public XMLThread(String url,Handler handler,TextView textView) { this.url=url; this.handler=handler; this.textView=textView; // TODO Auto-generated constructor stub } @Override public void run() { try { URL http=new URL(url); HttpURLConnection conn=(HttpURLConnection) http.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); Log.d("hjo","使用get方式 !"); InputStream in=conn.getInputStream();//获取服务器返回的输入流对象 Log.d("hjo","获取数据!"); /*解析xml文件*/ //使用Android原生的解析方式 XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); XmlPullParser parser=factory.newPullParser(); //解析格式为UTF-8 parser.setInput(in,"UTF-8"); int evenType=parser.getEventType(); final List<Gril> list=new ArrayList<Gril>(); Gril gril=null; while (evenType!=XmlPullParser.END_DOCUMENT) { String data=parser.getName();//获取标签的一个名字 // Log.d("hjo",data); switch (evenType) { case XmlPullParser.START_TAG://如果处在标签的开头例:<girl> if ("girl".equals(data)) { gril=new Gril(); } if ("age".equals(data)) { gril.setAge(Integer.parseInt(parser.nextText()));//将xml文件中的数据取出 并设置给gril } if ("school".equals(data)) { gril.setSchool(parser.nextText());//将xml文件中的数据取出 并设置给gril } if ("name".equals(data)) { gril.setName(parser.nextText());//将xml文件中的数据取出 并设置给gril } break; case XmlPullParser.END_TAG://如果处在标签的结尾例:</girl> if ("girl".equals(data)&&gril!=null) { list.add(gril); } break; } evenType=parser.next();//注意 这里不会自动获取下一个 所有需要手动执行获取下一步代码 } handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText(list.toString()); } }); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
MainActivity.java代码:
import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Handler handler=new Handler(); TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.textview); String url="http://localhost:8080/web3/gril.xml"; XMLThread thread=new XMLThread(url,handler,textView); thread.start(); } }
将获取到的数据在TextView上输出,layout_mian.xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.pss.xmlthread.MainActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
最后加上访问网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
运行效果:
原文链接:https://www.f2er.com/xml/295663.html