package com.jbridge.xml.service
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import com.jbridge.xml.domain.Person;
import android.util.Xml;
public class PullPersonService {
public static List<Person> getPersons(InputStream inStream)
throws Exception {
List<Person> persons = null;
Person person = null;
// 创建Pull解析
// xmlPullParserFactory=XmlPullParserFactory.newInstance();
// XmlPullParser pullParser =xmlPullParserFactory.newPullParser();
XmlPullParser pullParser = Xml.newPullParser();// 这行代码同上面两行
// 解析XML
pullParser.setInput(inStream, "UTF-8");
// 开始
int event = pullParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
// 文档开始
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
// 节点开始
case XmlPullParser.START_TAG:
if ("person".equals(pullParser.getName())) {
int id = new Integer(pullParser.getAttributeValue(0));
person = new Person();
person.setId(id);
}
if (person != null) {
if ("name".equals(pullParser.getName())) {
String name = pullParser.nextText();// 如果后面是Text元素,即返回它的值
person.setName(name);
} else if ("age".equals(pullParser.getName())) {
String age = pullParser.nextText();
person.setAge(new Short(age));
}
}
break;
// 节点结束
case XmlPullParser.END_TAG:
if ("person".equals(pullParser.getName())) {
persons.add(person);
person = null;
}
break;
}
event = pullParser.next();
}
return persons;
}
public static void save(List<Person> persons, OutputStream outStream) throws Exception{
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(outStream, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "persons");// serializer.startTag("", "persons");
for(Person person : persons){
serializer.startTag(null, "person");// serializer.startTag("", "person");
serializer.attribute(null, "id", person.getId().toString()); // serializer.attribute("", "id", person.getId().toString());
serializer.startTag(null, "name"); //serializer.startTag("", "name");
serializer.text(person.getName());
serializer.endTag(null, "name");// serializer.endTag("", "name");
serializer.startTag(null, "age"); //serializer.startTag("", "age");
serializer.text(person.getAge().toString());
serializer.endTag(null, "age"); // serializer.endTag("", "age");
serializer.endTag(null, "person");//serializer.endTag("", "person");
}
serializer.endTag(null, "persons"); //serializer.endTag("", "persons");
serializer.endDocument();
outStream.flush();
outStream.close();
}
public static void save(List<Person> persons,Writer write) throws Exception{
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(write);
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "persons");// serializer.startTag("", "persons");
for(Person person : persons){
serializer.startTag(null, "person");// serializer.startTag("", "person");
serializer.attribute(null, "id", person.getId().toString()); // serializer.attribute("", "id", person.getId().toString());
serializer.startTag(null, "name"); //serializer.startTag("", "name");
serializer.text(person.getName());
serializer.endTag(null, "name");// serializer.endTag("", "name");
serializer.startTag(null, "age"); //serializer.startTag("", "age");
serializer.text(person.getAge().toString());
serializer.endTag(null, "age"); // serializer.endTag("", "age");
serializer.endTag(null, "person");//serializer.endTag("", "person");
}
serializer.endTag(null, "persons"); //serializer.endTag("", "persons");
serializer.endDocument();
write.flush();
write.close();
}
}