package cn.csdn.service;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
import cn.csdn.domain.School;
import cn.csdn.domain.Student;
import cn.csdn.domain.Teacher;
public class PullSchoolService {
public static List<School> getSchoolList(InputStream in) throws Exception{
List<School> schoolList=null;
XmlPullParser parser=Xml.newPullParser();
parser.setInput(in,"utf-8");
School school=null;
Teacher teacher=null;
Student student=null;
List<Student> studentList=null;
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
schoolList=new ArrayList<School>();
break;
case XmlPullParser.START_TAG:
if("school".equals(parser.getName())){
school=new School();
school.setLevel(parser.getAttributeValue(0));
studentList=new ArrayList<Student>();
}else if("teacherName".equals(parser.getName())){
teacher=new Teacher();
teacher.setName(parser.nextText());
}else if("teacherAge".equals(parser.getName())){
teacher.setAge(Integer.parseInt(parser.nextText()));
}else if("studentName".equals(parser.getName())){
student=new Student();
student.setName(parser.nextText());
}else if("studentAge".equals(parser.getName())){
student.setAge(Integer.parseInt(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
if("teacher".equals(parser.getName())&&teacher!=null){
school.setTeacher(teacher);
teacher=null;
}else if("student".equals(parser.getName())&&student!=null){
studentList.add(student);
student=null;
}else if("school".equals(parser.getName())&&school!=null){
school.setStudent(studentList);
schoolList.add(school);
school=null;
studentList=null;
}
break;
}
// next方法的导致进行下一个节点的解析,同时返回下一个节点解析的事件类型代码、
eventType = parser.next();
}
return schoolList;
}
public static void writerPersonList(List<School> schoolList,Writer writer) throws Exception{
XmlSerializer serializer=Xml.newSerializer();
serializer.setOutput(writer);
//定入
serializer.startDocument("utf-8", true);
//写入根标签的起始
serializer.startTag(null, "schools");
for(School school:schoolList){
serializer.startTag(null, "school");
//添加id属性
serializer.attribute(null, "level", school.getLevel());
serializer.startTag(null, "teacher");
//老师姓名
serializer.startTag(null, "teacherName");
serializer.text(school.getTeacher().getName());
serializer.endTag(null, "teacherName");
//老师年龄
serializer.startTag(null, "teacherAge");
serializer.text(school.getTeacher().getAge()+"");
serializer.endTag(null, "teacherAge");
serializer.endTag(null, "teacher");
for(Student list:school.getStudent()){
serializer.startTag(null, "student");
//学生姓名
serializer.startTag(null, "studentName");
serializer.text(list.getName());
serializer.endTag(null, "studentName");
//学生年龄
serializer.startTag(null, "studentAge");
serializer.text(list.getAge()+"");
serializer.endTag(null, "studentAge");
serializer.endTag(null, "student");
}
serializer.endTag(null, "school");
}
//结束标签
serializer.endTag(null, "schools");
//结束文档的写入
serializer.endDocument();
writer.flush();
writer.close();
}
}