首页 > 学院 > 开发设计 > 正文

使用pull解析器解析和生成xml文件

2019-11-09 18:14:41
字体:
来源:转载
供稿:网友

解析的xml文件如下 person.xml

<?xml version="1.0" encoding="utf-8"?><persons> <person id="20"> <name>xiaoming</name> <age>23</age> </person> <person id="21"> <name>xiaoli</name> <age>27</age> </person></persons>

1.第一步 编写javaBean : Person.java

public class Person { PRivate String name; private int age; private int id; public Person() { } public Person(String name, int age, int id) { this.name = name; this.age = age; 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; } public int getId() { return id; } public void setId(int id) { this.id = id; }}

界面设计 直接展示布局: 这里写图片描述

读取XML和写入XML 功能实现

public class MainActivity extends AppCompatActivity { Button bt_readxml,bt_writexml; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_readxml=(Button)findViewById(R.id.bt_readxml); bt_writexml=(Button)findViewById(R.id.bt_writexml); //读取XML bt_readxml.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AssetManager assetManager= getResources().getAssets(); try { InputStream xml=assetManager.open("person.xml"); List<Person> persons= PersonService.getPersons(xml); for (Person person:persons){ Log.i("MainActivity","id:"+person.getId()+" name:"+person.getName()+" age:"+person.getAge()); } }catch (Exception e){ e.printStackTrace(); } } }); //写入XML bt_writexml.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<Person> persons=new ArrayList<Person>(); Person person1=new Person("lili",23,40); Person person2=new Person("mimi",19,24); persons.add(person1); persons.add(person2); File file=new File(getFilesDir(),"person.xml"); try { FileOutputStream out = new FileOutputStream(file); PersonService.savePersons(persons, out); } catch (Exception e){ e.printStackTrace(); } } }); }}

4.XML文件读取和写入的逻辑 PersonService.java如下:

public class PersonService { /** * 读取XML文件 * @param xml * @return * @throws Exception */ public static List<Person> getPersons(InputStream xml) throws Exception{ List<Person> persons=null; XmlPullParser parser= Xml.newPullParser(); //得到PULL解析器 parser.setInput(xml,"UTF-8");//设置输入流的编码格式 UTF-8 Person person=null; int event=parser.getEventType(); while (event!=XmlPullParser.END_DOCUMENT){ switch (event){ case XmlPullParser.START_DOCUMENT: persons=new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if("person".equals(parser.getName())){ int id =new Integer(parser.getAttributeValue(0)); person=new Person(); person.setId(id); } if("name".equals(parser.getName())){ String name=parser.nextText(); person.setName(name); } if("age".equals(parser.getName())){ int age=new Integer(parser.nextText()); person.setAge(age); } break; case XmlPullParser.END_TAG: if("person".equals(parser.getName())){ persons.add(person); person=null; } break; default:break; } event=parser.next(); } return persons; } /** * 写XML文件 * @param persons * @param out * @throws Exception */ public static void savePersons(List<Person> persons, OutputStream out) throws Exception{ XmlSerializer xml= Xml.newSerializer(); xml.setOutput(out,"UTF-8"); xml.startDocument("UTF-8",true); xml.startTag(null,"Persons"); for(Person person:persons){ xml.startTag(null,"Person"); xml.attribute(null,"id",String.valueOf(person.getId())); xml.startTag(null,"name"); xml.text(person.getName()); xml.endTag(null,"name"); xml.startTag(null,"age"); xml.text(Integer.toString(person.getAge())); xml.endTag(null,"age"); xml.endTag(null,"Person"); } xml.endTag(null,"Persons"); xml.endDocument(); out.flush(); out.close(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表