在Android中XML解析是一种很重要的储存文本信息的的方式
XML解析有三中方式:
- SAX 解析器
- DOM 解析器
- pull 解析器
在这里我主要写的是PULL解析器解析XML,在Android中集成了pull解析器,所以从侧面凸显比较推荐使用pull解析器。
我将解析文件存放于assets目录下
解析XML内容:
<?xml version="1.0" encoding="utf-8"?>
<persons>
<person id= "23">
<name>liming</name>
<age>30</age>
</person>
<person id= "20">
<name>zahngxiaoxiao</name>
<age>25</age>
</person>
</persons>
解析类PersonService类代码:
package com.example.administrator.foundationdemo.xmlparsepull.service;
import android.util.Xml;
import com.example.administrator.foundationdemo.xmlparsepull.domain.Person;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/12/2.
*/
public class PersonService {
public static List<Person> getPersonXML (InputStream xml) throws Exception{
List<Person> persons = null;
Person person = null;
//可通过pull解析器工场得到实例方法==》 XmlPullParserFactory.newInstance().newPullParser();
XmlPullParser pullParser = Xml.newPullParser();//通Xml过帮助类获得pull解析器实例
pullParser.setInput(xml,"UTF-8");//为pull解析器设置要解析的XML数据
/**分析:
* pull解析器会将数据读到一个 char[] = {-<..XML..>-}中
* pull解析器会自动读取开始段(及第一段“<?xml version="1.0" encoding="utf-8"?>”)类容,
* 从而判断是否符合XML开始类容的某一个语法,并触发对应语法所对应的事件
*/
int exent = pullParser.getEventType();//第一个事件及开始的事件
while (exent != XmlPullParser.END_DOCUMENT){
switch (exent){
case XmlPullParser.START_DOCUMENT://DOCUMENT(开始事件)
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG: //节点开始的TAG
if ("person".equals(pullParser.getName())){
int id = Integer.parseInt(pullParser.getAttributeValue(0));
person = new Person();
person.setId(id);
}
if ("name".equals(pullParser.getName())){
String name = pullParser.nextText();
person.setName(name);
}
if ("age".equals(pullParser.getName())){
int age = Integer.parseInt(pullParser.nextText());
person.setAge(age);
}
break;
case XmlPullParser.END_TAG: //节点结束的TAG
if ("person".equals(pullParser.getName())){
persons.add(person);
person = null;
}
break;
}
//当解析完开始事件之后,PLL解析器不会主动向下解析,
// 需要调用如下(.next())API,调用一次向下解析一步
exent = pullParser.next();
}
return persons;
}
}
解析类容生成子类的person类代码:
package com.example.administrator.foundationdemo.xmlparsepull.domain;
/**
* Created by Administrator on 2016/12/2.
*/
public class Person {
private int id;
private String name ;
private int 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;
}
}
Activity代码:
package com.example.administrator.foundationdemo.xmlparsepull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.example.administrator.foundationdemo.R;
import com.example.administrator.foundationdemo.xmlparsepull.domain.Person;
import com.example.administrator.foundationdemo.xmlparsepull.service.PersonService;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class XMLParsePullActivity extends AppCompatActivity {
TextView xml_parse_text ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
xmlParse();
}
private void init(){
setContentView(R.layout.activity_xmlparse_pall);
xml_parse_text = (TextView) findViewById(R.id.xml_parse_text);
}
private void xmlParse(){
try {
InputStream xml = this.getResources().getAssets().open("person.xml");// 获取assets下XM文件输出流
List<Person> persons = PersonService.getPersonXML(xml);
for (Person person : persons ){
String text = xml_parse_text.getText()+" \n id:"+person.getId()+"\n name:"+person.getName()+"\nage:"+person.getAge();
Log.d("FLY",text);
xml_parse_text.setText(text);
}
} catch (Exception e) {
Log.d("FLY","出错了"+e);
e.printStackTrace();
}
}
}
希望对你们有帮助,谢谢!!!