Java 操作 XML(12)--XPP3 使用
XPP3(XML Pull Parser)是一个非常快的XML解析器,它可以很方便地解析 XML 文档。本文主要介绍 XPP3 的基本使用,文中所使用到的软件版本:Java 1.8.0_181、xpp3 1.4.1c。
1、引入依赖
<dependency> <groupId>xpp3</groupId> <artifactId>xpp3</artifactId> <version>1.1.4c</version> </dependency>
2、XML样例文件
<?xml version="1.0" encoding="utf-8" ?> <school:grade xmlns:school="http://www.w3.org/TR/html4/school/"> <school:student rollno="1" school:age="10"> <school:firstname>cxx1</school:firstname> <lastname>Bob1</lastname> <nickname>stars1</nickname> <marks>85</marks> </school:student> <student rollno="2"> <firstname>cxx2</firstname> <lastname>Bob2</lastname> <nickname>stars2</nickname> <marks>85</marks> </student> <student rollno="3"> <firstname>cxx3</firstname> <lastname>Bob3</lastname> <nickname>stars3</nickname> <marks>85</marks> </student> </school:grade>
3、XPP3使用例子
package com.abc.demo.general.xml; import org.apache.commons.lang.StringUtils; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import java.util.ArrayList; import java.util.List; /** * Xpp3使用 */ public class Xpp3Case { private static Logger logger = LoggerFactory.getLogger(Xpp3Case.class); /** * 解析xml * @throws Exception */ @Test public void parse() throws Exception { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); xpp.setInput(Xpp3Case.class.getResourceAsStream("student.xml"), "utf-8"); List<Student> students = new ArrayList<>(); Student student = null; String text = ""; int eventType = xpp.getEventType(); while (true) { if (eventType == XmlPullParser.START_DOCUMENT) { logger.info("Start of Document"); } else if (eventType == XmlPullParser.START_TAG) { if ("student".equals(xpp.getName())) { student = new Student(); students.add(student); student.setRollno(Integer.parseInt(xpp.getAttributeValue("", "rollno"))); String age = xpp.getAttributeValue("http://www.w3.org/TR/html4/school/", "age"); if (StringUtils.isNotBlank(age)) { student.setAge(Integer.parseInt(age)); } } } else if (eventType == XmlPullParser.END_TAG) { if ("firstname".equals(xpp.getName())) { student.setFirstname(text); } else if ("lastname".equals(xpp.getName())) { student.setLastname(text); } else if ("nickname".equals(xpp.getName())) { student.setNickname(text); } else if ("marks".equals(xpp.getName())) { student.setMarks(text); } } else if (eventType == XmlPullParser.TEXT) { text = xpp.getText(); } else if (eventType == XmlPullParser.END_DOCUMENT) { logger.info("End of Document"); break; } eventType = xpp.next(); } logger.info(students.toString()); } }
Student实体类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.abc.demo.general.xml; /** * 学生 */ public class Student { private int rollno; private int age; private String firstname; private String lastname; private String nickname; private String marks; public Student() {} public Student(int rollno, int age, String firstname, String lastname, String nickname, String marks) { this.rollno = rollno; this.age = age; this.firstname = firstname; this.lastname = lastname; this.nickname = nickname; this.marks = marks; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getMarks() { return marks; } public void setMarks(String marks) { this.marks = marks; } @Override public String toString() { return "Student{" + "rollno=" + rollno + ", age=" + age + ", firstname='" + firstname + '\'' + ", lastname='" + lastname + '\'' + ", nickname='" + nickname + '\'' + ", marks='" + marks + '\'' + '}'; } }