javaxml多层解析_xml多层嵌套解析⽅法SAX⽅式(包括⽹络
请求)
启⽤异步类获取数据
package asynctask;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;网络上xml是什么意思
import java.util.List;
l.parsers.ParserConfigurationException;
l.parsers.SAXParser;
l.parsers.SAXParserFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.ity.UrlEncodedFormEntity;
import org.apache.hods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.ssage.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
l.sax.SAXException;
import com.bwie.http_img_xml_caipu.ItemActivity;
import adapter.HomeAdapter;
import defaulthander.HomeDefaultHander;
import com.bwie.vo.DataVO;
import com.bwie.vo.StepsVO;
t.Context;
t.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MyAsyncTask extends AsyncTask> {
private Context context;
private ListView lv;
private String str_ed;
private List datalist;
public MyAsyncTask(Context context, ListView lv, String str_ed) {
super();
this.lv = lv;
this.str_ed = str_ed;
}
@Override
protected List arg0) {
// TODO Auto-generated method stub
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(arg0[0]);
List list=new ArrayList();
list.add(new BasicNameValuePair("key", "3a3d0fa15180459cbf022d5d77e50de5")); list.add(new BasicNameValuePair("menu", str_ed));
list.add(new BasicNameValuePair("rn", "10"));
list.add(new BasicNameValuePair("pn", "3"));
list.add(new BasicNameValuePair("dtype", "xml"));
try {
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,HTTP.UTF_8);
post.setHeader("Content_Type", "application/x-www-form-urlencoded;charset=utf-8"); post.setEntity(entity);
HttpResponse response = ute(post);
if (StatusLine().getStatusCode()==200) {
InputStream Entity().getContent();
SAXParserFactory wInstance();
SAXParser wSAXParser();
HomeDefaultHander dh=new HomeDefaultHander();
parser.parse(is, dh);
datalist = dh.getdatalist();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datalist;
}
@Override
protected void onPostExecute(final List result) {
/
/ TODO Auto-generated method stub
lv.setAdapter(new HomeAdapter(context, result));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent(context, ItemActivity.class);
intent.putExtra("list",((arg2).steps); context.startActivity(intent);
}
});
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected void values) {
// TODO Auto-generated method stub
}
}
⾃定义类
package defaulthander;
import java.util.ArrayList;
import java.util.List;
l.sax.Attributes;
l.sax.SAXException;
l.sax.helpers.DefaultHandler;
import android.util.Log;
import com.bwie.vo.DataVO;
import com.bwie.vo.StepsVO;
public class HomeDefaultHander extends DefaultHandler { private List data;
private int mun=0;
private String flag;
private DataVO dataVO;
private StepsVO stepsVO;
//开始解析⽂档
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
data = new ArrayList();
Log.e("TAG", "开始解析⽂档");
}
//开始解析标签
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
Log.e("TAG", "解析标签开始");
flag = localName;
if (localName.equals("item")&&mun==0) {
dataVO = new DataVO();
mun=1;
}else if (localName.equals("item")&&mun==2) {
stepsVO = new StepsVO();
mun=2;
}else if(localName.equals("steps")){
dataVO.steps=new ArrayList();
mun=2;
}
}
//解析⽂本
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String text=new String(ch, start, length);
Log.e("TAG", "解析⽂本"+text);
if (flag.equals("title")) {
dataVO.setTitle(text);
} else if (flag.equals("tags")){
dataVO.setTags(text);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。