android 解析xml

时间:2017-06-13 07:43:12
【文件属性】:

文件名称:android 解析xml

文件大小:726B

文件格式:XML

更新时间:2017-06-13 07:43:12

pullparser

package com.test; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import com.test.webservice.WebService; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ private TextView text=null; private Button Btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView)findViewById(R.id.TextView); Btn = (Button) this.findViewById(R.id.yyy); Btn.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { try{ String name=""; String id=""; XmlPullParser pullParser = WebService.getXMLPullParser("soap.xml", null); List list = new ArrayList(); int event = pullParser.getEventType(); while(event != XmlPullParser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_TAG: if("ID".equalsIgnoreCase(pullParser.getName())){ name = pullParser.nextText(); } if("Name".equalsIgnoreCase(pullParser.getName())){ id = pullParser.nextText(); list.add(name+id); } break; } event = pullParser.next(); } String rr=""; for(String temp:list){ rr+=temp; } text.setText(rr); }catch(Exception er){ Toast.makeText(MainActivity.this,R.string.error, 200).show(); } }}); } }


网友评论