Android中怎么使用pull解析xml
本篇文章为大家展示了Android中怎么使用 pull解析xml,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联从2013年开始,先为铁锋等服务建站,铁锋等地企业,进行企业商务咨询服务。为铁锋企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
persons.xml
jame 18 Tom 20 Jack 16 Rose 26
activity布局文件
activity_main.xml
javaBean:
public class Person { private int id; private String name; private int age; public Person() { super(); } public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
主activity:
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Xml; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private TextView tv_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.tv_content); findViewById(R.id.bt_read).setOnClickListener(this); } @Override public void onClick(View v) { try { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Listpersons = new ArrayList (); Person person = null; File sd_file = Environment.getExternalStorageDirectory(); File file = new File(sd_file, "persons.xml"); InputStream inputStream = new FileInputStream(file); // 获取xml Pull解析器 XmlPullParser pullParser = Xml.newPullParser(); // 解析文件 pullParser.setInput(inputStream, "utf-8"); // 得到事件类型(START_DOCUMENT,END_DOCUMENT,START_TAG, END_TAG, TEXT, // etc) int eventType = pullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: // 获取标签的名称 String tag_name = pullParser.getName(); if ("person".equals(tag_name)) { person = new Person(); persons.add(person); // String value = pullParser.getAttributeValue(null, // "id"); String value = pullParser.getAttributeValue(0); person.setId(Integer.parseInt(value)); } else if ("name".equals(tag_name)) { // pullParser.next(); // String text = pullParser.getText(); String text = pullParser.nextText(); person.setName(text); } else if ("age".equals(tag_name)) { // pullParser.next(); // String text = pullParser.getText(); String text = pullParser.nextText(); person.setAge(Integer.parseInt(text)); } break; default: break; } eventType = pullParser.next(); } tv_content.setText(persons.toString()); } } catch (Exception e) { e.printStackTrace(); } } }
上述内容就是Android中怎么使用 pull解析xml,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站题目:Android中怎么使用pull解析xml
分享地址:http://azwzsj.com/article/geddcs.html