亲宝软件园·资讯

展开

android pull Android pull解析xml的实现方法

chuyouyinghe 人气:0
想了解Android pull解析xml的实现方法的相关内容吗,chuyouyinghe在本文为您仔细讲解 android pull的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,pull解析xml的方法,Android,pull解析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(); 
    } 
  } 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论