Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pull to parse xml in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use pull to parse xml in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use pull to parse xml in Android".

Persons.xml

Jame 18 Tom 20 Jack 16 Rose 26

Activity layout file

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 + "]";}}

Master 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 persons = new ArrayList (); Person person = null; File sd_file = Environment.getExternalStorageDirectory (); File file = new File (sd_file, "persons.xml"); InputStream inputStream = new FileInputStream (file) / / get xml Pull parser XmlPullParser pullParser = Xml.newPullParser (); / / parse file pullParser.setInput (inputStream, "utf-8"); / / get event type (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: / / get the name of the 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 () } Thank you for reading, this is the content of "how to use pull to parse xml in Android". After the study of this article, I believe you have a deeper understanding of how to use pull to parse xml in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report