1、以下是使用SSH框架生成xml
一、第一步写一个实体类(Person)
package com.zking.entity;
public class Person {
private int pid;
private String pname;
private int page;
public Person() {
super();
}
public Person(String pname, int page) {
super();
this.pname = pname;
this.page = page;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", page=" + page + "]";
}
}
二、第二 配置Struts配置文件
/login.jsp
三、展示数据库数据(xml)
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
${p.pname}
${p.page}
用listview展示数据
MainActivity
package com.example.android_parsexml;
import android.app.ProgressDialog;
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 android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List PersonList=new ArrayList<>();
private ListView listview;
private ProgressDialog progressDialog;
private Myadater myadater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化进度条对话框
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("拼命加载中......");
listview = (ListView) findViewById(R.id.listview);
//实例化适配器
myadater = new Myadater();
listview.setAdapter(myadater);
}
class Myadater extends BaseAdapter{
@Override
public int getCount() {
return PersonList.size();
}
@Override
public Object getItem(int i) {
return PersonList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout layout=new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView textViewpid=new TextView(MainActivity.this);
textViewpid.setText(PersonList.get(i).getPid()+"");
TextView textViewName=new TextView(MainActivity.this);
textViewName.setText(PersonList.get(i).getPname());
TextView textViewsex=new TextView(MainActivity.this);
textViewsex.setText(PersonList.get(i).getPage()+"");
layout.addView(textViewpid);
layout.addView(textViewName);
layout.addView(textViewsex);
return layout;
}
}
public void content(View view){
new MyTask().execute();
}
class MyTask extends AsyncTask{
private Person person;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected Object doInBackground(Object[] objects) {
//获取网络数据XML
//01.确定网络路径http://localhost:8080/mySSH/personActionadd.action
String path="http://192.168.23.1:8080/mySSH/personActionaddperson.action";
try {
//02.实例化URL
URL url=new URL(path);
//03获取连接对象
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//04.设置请求方式
connection.setRequestMethod("GET");
//05.设置请求连接超时的时间(优化)
connection.setConnectTimeout(5000);
//06.获取响应码,结果码
int code=connection.getResponseCode();
if (code==200){
//07.获取服务器返回过来的数据
InputStream is=connection.getInputStream();
//解析PULL
XmlPullParser xpp= Xml.newPullParser();
xpp.setInput(is,"UTF-8");
int type=xpp.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
//获取开始标签的名字
String startTagName=xpp.getName();
Log.i("test",""+"startTagName"+startTagName);
if("person".equals(startTagName)){
person = new Person();
//获取name属性值
int pid=Integer.parseInt(xpp.getAttributeValue(0));
Log.i("test",""+pid);
person.setPid(pid);
}else if("pname".equals(startTagName)){
//获取sex的文本值
String pname=xpp.nextText();
Log.i("test",""+pname);
person.setPname(pname);
}else if("page".equals(startTagName)){
//获取sex的文本值
int page=Integer.parseInt(xpp.nextText());
Log.i("test",""+page);
person.setPage(page);
}
break;
case XmlPullParser.END_TAG:
//获取到结束标签的名字
String endTagName=xpp.getName();
Log.i("test",""+endTagName);
if("person".equals(endTagName)){
PersonList.add(person);
}
break;
}
type=xpp.next();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//通知适配器发生改变
myadater.notifyDataSetChanged();
progressDialog.cancel();
}
}
}
<uses-permission android:name="android.permission.INTERNET">uses-permission>