Android pull解析xml的实现方法
程序员文章站
2023-11-28 21:19:10
android pull解析xml的实现方法
资源文件:
persons.xml
android pull解析xml的实现方法
资源文件:
persons.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <persons> <person id="101"> <name>jame</name> <age>18</age> </person> <person id="102"> <name>tom</name> <age>20</age> </person> <person id="103"> <name>jack</name> <age>16</age> </person> <person id="104"> <name>rose</name> <age>26</age> </person> </persons>
activity布局文件
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativepackage}.${activityclass}" > <textview android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <button android:id="@+id/bt_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取xml文件" /> </linearlayout>
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)) { list<person> persons = new arraylist<person>(); 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(); } } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: 详解Vue组件之作用域插槽