前端之家收集整理的这篇文章主要介绍了
xml文件pull的解析,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//分三部分1是得到实体类.2是得到干活的类personservice.3是在主活动调用干活的类做输出.
package cn.itcast.xml.domain;
public class Person {
private int id;
private String name;
private int age;
public Person(){
}
public Person(int id,String name,int age) {
this.id = id;
this.name = name;
this.setAge(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) {
if (age > 0 && age < 100) {
this.age = age;
}else{
this.age = 0;
}
}
}
<pre class="java" name="code">package cn.itcast.xml.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast;
import cn.itcast.xml.domain.Person;
public class PersonService {
private Context context;
public PersonService(Context context) {
this.context = context;
}
/**
* 把person.xml的输入流 解析 转化成list集合
* @param filename assets目录下的文件名
* @return
*/
public List<Person> getPersons(String filename){
AssetManager manager = context.getAssets();
try {
InputStream is = manager.open(filename);
//在android下使用pull解析xml文件
//1.获取pull解析器的实例
XmlPullParser parser = Xml.newPullParser();
//2.设置解析器的一些参数
parser.setInput(is,"utf-8");
// 获取pull解析器对应的事件类型
int type = parser.getEventType();
Person person = null;
List<Person> persons = new ArrayList<Person>();
while(type!=XmlPullParser.END_DOCUMENT){
if(type==XmlPullParser.START_TAG){
if("person".equals(parser.getName())){
person = new Person();
int id =Integer.parseInt( parser.getAttributeValue(0));
person.setId(id);
}else if("name".equals(parser.getName())){
String name = parser.nextText();
person.setName(name);
}else if("age".equals(parser.getName())){
int age = Integer.parseInt( parser.nextText());
person.setAge(age);
}
}
if(type==XmlPullParser.END_TAG){
if("person".equals(parser.getName())){
persons.add(person);
person = null;
}
}
type = parser.next();
}
return persons;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context,"获取person.xml失败",Toast.LENGTH_SHORT).show();
return null;
}
}
/**
* 把persons集合里面的内容写到xml文件里面
* @param persons person的集合
* @return
*/
public boolean savePersonToXml(List<Person> persons){
try {
XmlSerializer serializer = Xml.newSerializer();
// /sdcard/person.xml
// mount unmount 文件系统的权限
File file = new File(Environment.getExternalStorageDirectory(),"person.xml");
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos,"utf-8");
serializer.startDocument("utf-8",true);
serializer.startTag(null,"persons");
for(Person person:persons){
serializer.startTag(null,"person");
serializer.attribute(null,"id",person.getId()+"");
serializer.startTag(null,"name");
serializer.text(person.getName());
serializer.endTag(null,"name");
serializer.startTag(null,"age");
serializer.text(person.getAge()+"");
serializer.endTag(null,"age");
serializer.endTag(null,"person");
}
serializer.endTag(null,"persons");
serializer.endDocument();
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
<pre class="java" name="code">package cn.itcast.xml;
import java.util.List;
import cn.itcast.xml.domain.Person;
import cn.itcast.xml.service.PersonService;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.tv);
PersonService service = new PersonService(this);
List<Person> persons = service.getPersons("person.xml");
StringBuffer sb = new StringBuffer();
for(Person person : persons){
String age = "年龄"+ person.getAge();
String name =person.getName();
String id = "id="+ person.getId();
sb.append(name +" "+ age+" "+id);
}
tv.setText( sb.toString());
}
}
package cn.itcast.xml.test;
import java.util.ArrayList;
import java.util.List;
import cn.itcast.xml.domain.Person;
import cn.itcast.xml.service.PersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class TestPersonService extends AndroidTestCase {
private static final String TAG = "TestPersonService";
public void testGetPersons() throws Exception{
PersonService service = new PersonService(getContext());
List<Person> persons = service.getPersons("person.xml");
assertEquals(2,persons.size());
Log.i(TAG,persons.get(0).getName());
}
public void testSavePersons() throws Exception{
PersonService service = new PersonService(getContext());
List<Person> persons = new ArrayList<Person>();
Person p1 = new Person(21,"张三",18);
Person p2 = new Person(27,"李四",29);
Person p3 = new Person(29,"wangwu",-3);
persons.add(p1);
persons.add(p2);
persons.add(p3);
assertEquals(true,service.savePersonToXml(persons)) ;
}
}
原文链接:https://www.f2er.com/xml/298428.html