android 使用pull 方式解析 xml文件
1、创建一个.xml 文件,用来描述事物的信息:computer.xml
package com.example.layout;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import com.example.bean.Computer;import com.example.util.MyXmlPull;public class MainActivity extends Activity { private LinearLayout line1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); line1 = (LinearLayout) this.findViewById(R.id.line1); Button btn = new Button(this); btn.setText("点击"); line1.addView(btn, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { List<Computer> list = MyXmlPull.getComputersInfo(getClass().getClassLoader().getResourceAsStream("computer.xml")); for (Computer c : list) { Log.i("mark", c.toString()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); }}
note:本程序中的computer.xml 是放置在src 的根目录下