package pull解析parse1;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class Xml {
public static void main(String[] args) {
List students = null;
Student student = null;
try {// 获取工厂41b
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();// 获取解析器
XmlPullParser parser = factory.newPullParser();// 设置输入流,也XML文件
FileInputStream file = new FileInputStream("helloxml.xml");
parser.setInput(file, "UTF-8");// 获取时间类型
int eventType = parser.getEventType();// 循环取出数据
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if ("students".equals(parser.getName())) {
System.out.println("标签开始");students = new ArrayList();
} else if ("student".equals(parser.getName())) {
student = new Student();
String id = parser.getAttributeValue(0);
student.setName(id);
System.out.println("student的ID是" + id);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
student.setName(name);
System.out.println("name = " + name);
} else if ("age".equals(parser.getName())) {
String age = parser.nextText();
student.setName(age);
System.out.println("age=" + age);
}
break;
}
eventType = parser.next();
}
System.out.println(students);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过此题的核心主要是关于xlm文件的设置
xml文件怎么处理,Star document ,start text charactor end text ,end document 这几步来进行处理。
后续就要依靠部分面向对象的知识来进行梳理了。