dom4j解析xml实例

dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异、功能强大和极易使用等特点

所用jar包:dom4j-1.6.1.jar

需要解析的xml文件:people.xml

 

Xml代码  收藏代码
  1. <people city="shenzhen">  
  2.     <student name="milton" age="22"></student>  
  3.     <student name="lego" age="23"></student>  
  4.     <teacher name="bruce" age="27"></teacher>  
  5.     <teacher name="ziven" age="28"></teacher>  
  6. </people>  

首先根据xml文档构建javabean

People.java

Student.java

Teacher.java

 解析代码:

 

Java代码  收藏代码
  1. package com.test.xml;  
  2.   
  3. import java.io.File;  
  4. import java.net.URISyntaxException;  
  5. import java.util.ArrayList;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8.   
  9. import org.dom4j.Document;  
  10. import org.dom4j.DocumentException;  
  11. import org.dom4j.Element;  
  12. import org.dom4j.io.SAXReader;  
  13.   
  14. import com.test.xml.element.People;  
  15. import com.test.xml.element.Student;  
  16. import com.test.xml.element.Teacher;  
  17.   
  18. public class XmlParser {  
  19.     @SuppressWarnings("unchecked")  
  20.     public static People parseXml(String xmlPath){  
  21.         File xmlFile = new File(xmlPath);  
  22.         System.out.println(xmlFile.getPath());  
  23.         if(xmlFile.exists()){  
  24.             SAXReader reader = new SAXReader();  
  25.             People people = new People();  
  26.             try {  
  27.                 //读入文档流  
  28.                 Document document = reader.read(xmlFile);  
  29.                 //获取根节点  
  30.                 Element root = document.getRootElement();  
  31.                   
  32.                 List<Student> students = new ArrayList<Student>();  
  33.                 List<Teacher> teachers = new ArrayList<Teacher>();  
  34.                 people.setCity(root.attributeValue("city"));  
  35.                 //解析student节点  
  36.                 for(Iterator iterator = root.elementIterator("student");iterator.hasNext();){  
  37.                     Element eStudent = (Element) iterator.next();  
  38.                     Student student = new Student(eStudent.attributeValue("name"), Integer.parseInt(eStudent.attributeValue("age")));  
  39.                     students.add(student);  
  40.                 }  
  41.                 //解析teacher节点  
  42.                 for(Iterator iterator = root.elementIterator("teacher");iterator.hasNext();){  
  43.                     Element eTeacher = (Element) iterator.next();  
  44.                     Teacher teacher = new Teacher(eTeacher.attributeValue("name"), Integer.parseInt(eTeacher.attributeValue("age")));  
  45.                     teachers.add(teacher);  
  46.                 }  
  47.                 people.setStudent(students);  
  48.                 people.setTeacher(teachers);  
  49.                 System.out.println("parse success!");  
  50.             } catch (DocumentException e) {  
  51.                 e.printStackTrace();  
  52.             }  
  53.             return people;  
  54.         }else{  
  55.             System.out.println("file is not exist!");  
  56.             return null;  
  57.         }  
  58.     }  
  59.       
  60.     public static void main(String[] args) throws URISyntaxException{  
  61.         String xmlPath = "";  
  62.         xmlPath = "D:/Program Files/eclipse-jee/workspace/TEST/src/com/test/xml/people.xml";  
  63.         System.out.println("file full path is "+xmlPath);  
  64.         //解析people.xml  
  65.         People p = parseXml(xmlPath);  
  66.           
  67.         //处理解析结果  
  68.         System.out.println(p.getCity());  
  69.         List<Student> sList = p.getStudent();  
  70.         List<Teacher> tList = p.getTeacher();  
  71.         for(Student s:sList){  
  72.             System.out.println(s.getName()+"--"+s.getAge());  
  73.         }  
  74.         for(Teacher t:tList){  
  75.             System.out.println();  
  76.         }  
  77.     }  
  78. }  
Java代码  收藏代码
  1. 运行结果:  
Java代码  收藏代码
  1. parse success!  
  2. shenzhen  
  3. milton--22  
  4. lego--23  
posted @ 2014-07-29 10:23  岁月淡忘了谁  阅读(342)  评论(0编辑  收藏  举报