转自:http://salever.iteye.com/blog/710936
使用apche 的 betwixt,相关库为
Ø commons-beanutils.jar
Ø commons-betwixt-1.0-beta-1.jar
Ø commons-collections-3.2.1.jar
Ø commons-configuration-1.6.jar
Ø commons-digester.jar
Ø commons-lang-2.4.jar
Ø commons-logging.jar
比如解析如下XML:
- <?xml version="1.0" encoding="UTF-8" ?>
- <class id="1" name="class one">
- <students>
- <student name="Jime">
- <score maths="80" />
- </student>
- <student name="Abby">
- <score maths="100" />
- </student>
- </students>
- </class>
结构很简单,这里使用betwixt 方式进行解析。
首先封装XML 的元素信息,主要为3个类:
Class.java
- public class Class {
- private int id;
- private String name;
- private List<Student> students = new ArrayList<Student>();
- /**
- * Add a student.
- * @param student
- */
- public void addStudent(Student student){
- this.students.add(student);
- }
- /**
- * Remove a student.
- * @param student
- */
- public void removeStudent(Student student){
- this.students.remove(student);
- }
- /**
- * @return the id
- */
- public int getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(int id) {
- this.id = id;
- }
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the students
- */
- public List<Student> getStudents() {
- return students;
- }
- /**
- * @param students the students to set
- */
- public void setStudents(List<Student> students) {
- this.students = students;
- }
- }
Student.java
- public class Score {
- private int mathScore;
- /**
- * @return the mathScore
- */
- public int getMathScore() {
- return mathScore;
- }
- /**
- * @param mathScore the mathScore to set
- */
- public void setMathScore(int mathScore) {
- this.mathScore = mathScore;
- }
- }
都很简单,就不用多描述了。下面是每个类对应的XML 描述文件:
Class.betwixt:
- <?xml version="1.0" encoding="UTF-8"?>
- <info primitiveTypes="attribute">
- <element name="class">
- <attribute name="id" property="id"/>
- <attribute name="name" property="name"/>
- <element name="students">
- <element name="student" property="student"/>
- </element>
- <addDefaults/>
- </element>
- </info>
Student.betwixt:
- <?xml version="1.0" encoding="UTF-8"?>
- <info primitiveTypes="attribute">
- <element name="student">
- <attribute name="name" property="name"/>
- <element name="score" property="score"/>
- <addDefaults/>
- </element>
- </info>
Score.betwixt:
- <?xml version="1.0" encoding="UTF-8"?>
- <info primitiveTypes="attribute">
- <element name="score">
- <attribute name="maths" property="mathScore"/>
- <addDefaults/>
- </element>
- </info>
这些XML 文件名与类名相同,用来在XML 文件与实体类之间进行映射。具体的解析方法就很简单了。注意这些文件需要跟.java文件放到同一目录下。
- public static Object fromXml(InputStream in, Class clazz)
- throws XmlException {
- BeanReader beanReader = new BeanReader();
- Object object = null;
- try {
- beanReader.registerBeanClass(clazz);
- beanReader.getXMLIntrospector().setWrapCollectionsInElement(true);
- object = beanReader.parse(in);
- } catch (Exception e) {
- throw new XmlException(e);
- }
- return object;
- }
使用fromXml 解析文件:
- public static Class parseFromXml(String path) throws Exception {
- FileInputStream in;
- Class obj = null;
- try {
- in = new FileInputStream(path);
- obj = (Class) XmlUtil.fromXml(in, Class.class);
- } catch (FileNotFoundException e) {
- throw new Exception(e);
- } catch (Exception e) {
- throw new Exception(e);
- }
- return obj;
- }
输出为XML:
- public static void toXml(Object object, OutputStream out)
- throws Exception {
- try {
- BeanWriter beanWriter = new BeanWriter(out, "UTF-8");
- beanWriter.enablePrettyPrint();
- beanWriter.setIndent(" ");
- beanWriter.setEndOfLine(System.getProperty("line.separator"));
- beanWriter.writeXmlDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- beanWriter.getXMLIntrospector().setWrapCollectionsInElement(true);
- beanWriter.setWriteEmptyElements(false);
- beanWriter.write(object);
- beanWriter.close();
- } catch (Exception e) {
- throw new XmlException(e);
- }
- }
将Class 类的对象输出为XML:
- public static void writeToXml(Object object, String path)
- throws Exception {
- try {
- FileOutputStream out = new FileOutputStream(path);
- XmlUtil.toXml(object, out);
- out.close();
- } catch (FileNotFoundException e) {
- throw new Exception(e);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
完成XML 解析的过程很简单,就只有几句话而以,这对于大型复杂的XML 解析很有帮助,不需要亲自处理各种属性、元素等,因为各种.jar 文件已经封装好了。