XML Multilayer nested parsing method sax mode (including network requests)

Source: Internet
Author: User

Enable asynchronous class fetch data

Package com.bwie.myasynctask;

Import java.io.IOException;
Import Java.io.InputStream;
Import java.io.Serializable;
Import java.io.UnsupportedEncodingException;
Import java.util.ArrayList;
Import java.util.List;

Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.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.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.protocol.HTTP;
Import org.xml.sax.SAXException;

Import Com.bwie.http_img_xml_caipu. itemactivity;
Import Com.bwie.myadapter.HomeAdapter;
Import Com.bwie.mydefaulthander.HomeDefaultHander;
Import Com.bwie.vo.DataVO;
Import Com.bwie.vo.StepsVO;

Import Android.content.Context;
Import android.content.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<string, Integer, list<datavo>> {
Private context context;
Private ListView LV;
Private String str_ed;
Private list<datavo> DataList;


Public Myasynctask (context context, ListView LV, String str_ed) {
Super ();
This.context = context;
this.lv = LV;
this.str_ed = str_ed;
}

@Override
Protected list<datavo> doinbackground (String ... arg0) {
TODO auto-generated Method Stub
HttpClient client=new defaulthttpclient ();
HttpPost post=new HttpPost (arg0[0]);
List<namevaluepair> list=new arraylist<namevaluepair> ();
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 = Client.execute (POST);
if (Response.getstatusline (). Getstatuscode () ==200) {
InputStream is=response.getentity (). getcontent ();
SAXParserFactory factory=saxparserfactory.newinstance ();
SAXParser Parser=factory.newsaxparser ();
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<datavo> result) {
TODO auto-generated Method Stub
Super.onpostexecute (result);
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", (Serializable) Result.get (arg2). steps);
Context.startactivity (Intent);
}
});
}

@Override
protected void OnPreExecute () {
TODO auto-generated Method Stub
Super.onpreexecute ();
}

@Override
protected void Onprogressupdate (Integer ... values) {
TODO auto-generated Method Stub
Super.onprogressupdate (values);
}

}

Custom Classes

Package com.bwie.mydefaulthander;

Import java.util.ArrayList;
Import java.util.List;

Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;

Import Android.util.Log;

Import Com.bwie.vo.DataVO;
Import Com.bwie.vo.StepsVO;

public class Homedefaulthander extends DefaultHandler {
Private list<datavo> data;
private int mun=0;
Private String flag;
Private Datavo Datavo;
Private STEPSVO STEPSVO;
Start parsing documents
@Override
public void Startdocument () throws Saxexception {
TODO auto-generated Method Stub
Super.startdocument ();
data = new arraylist<datavo> ();
LOG.E ("TAG", "Start parsing document");
}
Start parsing labels
@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", "Parse tag start");
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<stepsvo> ();
mun=2;
}
}
Parsing text
@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", "Parse text" +text);
if (Flag.equals ("title")) {
Datavo.settitle (text);
} else if (Flag.equals ("tags")) {
Datavo.settags (text);
}else if (flag.equals ("Imtro")) {
Datavo.setimtro (text);
}else if (flag.equals ("ingredients")) {
Datavo.setingredients (text);
}else if (flag.equals ("burden")) {
Datavo.setburden (text);
}else if (flag.equals ("item") &&mun==1) {
Datavo.setalbums (text);
}else if (flag.equals ("img")) {
Stepsvo.setimg (text);
}else if (flag.equals ("step")) {
Stepsvo.setstep (text);
}
}
Parse Label end
@Override
public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
TODO auto-generated Method Stub
Super.endelement (URI, LocalName, QName);
if (Localname.equals ("item") &&mun==2) {
DataVO.steps.add (STEPSVO);
mun=2;
}else if (localname.equals ("item") &&mun==0) {
Data.add (Datavo);
mun=0;
}else if (localname.equals ("steps")) {
mun=0;
}
Flag= "";
}
Parse Document End
@Override
public void Enddocument () throws Saxexception {
TODO auto-generated Method Stub
Super.enddocument ();
}
Get the Outer collection
Public list<datavo> getdatalist () {
return data;

}

}

XML Multilayer nested parsing method sax mode (including network requests)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.