使用面向对象对XML进行解析:dom和dom4j的用法

这是一个比较简单的例子,主要是运用面向对象的思想,对XML文件进行解析,用dom和dom4j方法

<?xml version="1.0" encoding="utf-8"?>
<students>
    <student>
        <name>张三</name>
        <age>20</age>
        <school>北京大学</school>
    </student>
    <student>
        <name>李四</name>
        <age>18</age>
        <school>清华大学</school>
    </student>
</students>

代码如下:

学生对象类

package d20160624;

public class Student {
    private String name;
    private String age;
    private String school;
    
    public Student() {
    }

    public Student(String name, String age, String school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
    //打印学生信息
    public void showInfo(){
        System.out.println("姓名:"+this.name+"\n年纪:"+this.age+"\n学校:"+this.school);
        System.out.println("***********");
    }
    
}

dom解析:

 1 package d20160624;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import javax.xml.parsers.DocumentBuilder;
 7 import javax.xml.parsers.DocumentBuilderFactory;
 8 import javax.xml.parsers.ParserConfigurationException;
 9 
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.NodeList;
13 import org.xml.sax.SAXException;
14 
15 /*
16  * 使用DOM解析student.xml,输出打印学生信息
17  */
18 public class DomParseStudent {
19     private Document document;
20     private String name,age,school;
21     
22 
23     //    生成dom树
24     public void getDom(File file){
25 //        创建解析器工厂
26         DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
27         try {
28 //            创建解析器对象
29             DocumentBuilder db=dbf.newDocumentBuilder();
30 //            解析XML文件得到DOM树
31             this.document=db.parse(file);
32         } catch (ParserConfigurationException e) {
33             e.printStackTrace();
34         } catch (SAXException e) {
35             e.printStackTrace();
36         } catch (IOException e) {
37             e.printStackTrace();
38         }
39 
40 
41     }
42 //    解析XML
43     public void showEle(){
44         DomParseStudent dps=new DomParseStudent();
45         NodeList students=document.getElementsByTagName("student");
46         for(int i=0;i<students.getLength();i++){
47             Element student=(Element) students.item(i);
48             NodeList names=student.getElementsByTagName("name");
49             for(int j=0;j<names.getLength();j++){
50                 Element name=(Element) names.item(j);
51                 this.name=name.getTextContent();
52             }
53             NodeList ages=student.getElementsByTagName("age");
54             for(int k=0;k<ages.getLength();k++){
55                 Element age=(Element) ages.item(k);
56                 this.age=age.getTextContent();
57             }
58             NodeList schools=student.getElementsByTagName("school");
59             for(int p=0;p<schools.getLength();p++){
60                 Element school=(Element) schools.item(p);
61                 this.school=school.getTextContent();
62             }
63             
64             Student stu=new Student(name, age, school);
65             stu.showInfo();
66             
67         }
68     }
69     
70     public static void main(String[] args){
71         DomParseStudent dps=new DomParseStudent();
72         File file=new File("D:\\Java_study\\workspace\\myeclipse\\HomeWork\\src\\d20160624\\student.xml");
73         dps.getDom(file);
74         dps.showEle();
75     }
76 }

dom4j解析:

 1 package d20160624;
 2 
 3 import java.io.File;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 
 7 import org.dom4j.io.SAXReader;
 8 import org.dom4j.Document;
 9 import org.dom4j.DocumentException;
10 import org.dom4j.Element;
11 
12 /*
13  * 使用dom4j解析student.xml,输出打印学生信息
14  */
15 public class Dom4jStudent {
16     private Document document;
17     private String name,age,school;
18 //    生成dom树
19     public void getDom(File file){
20 //        创建SAXReader
21         SAXReader sax=new SAXReader();
22         try {
23 //            读取xml文件,生成dom树
24             this.document=sax.read(file);
25         } catch (DocumentException e) {
26             e.printStackTrace();
27         }
28     }
29     
30 //    读取XML文件
31     public void showXML(){
32         Element root=document.getRootElement();
33         List<Element> student=root.elements();
34         for(Iterator<Element> stuit=student.iterator();stuit.hasNext();){
35             Element stu=stuit.next();
36             List<Element> names=stu.elements("name");
37             for(Iterator<Element> name=names.iterator();name.hasNext();){
38                 Element nameel=name.next();
39                 this.name=nameel.getText();
40             }
41             List<Element> ages=stu.elements("age");
42             for(Iterator<Element> age=ages.iterator();age.hasNext();){
43                 Element ageel=age.next();
44                 this.age=ageel.getText();
45             }
46             List<Element> schools=stu.elements("school");
47             for(Iterator<Element> school=schools.iterator();school.hasNext();){
48                 Element schoolel=school.next();
49                 this.school=schoolel.getText();
50             }
51             Student stu1=new Student(name, age, school);
52             stu1.showInfo();
53         }
54     }
55     
56     public static void main(String[] args){
57         Dom4jStudent d4j=new Dom4jStudent();
58         File file=new File("D:\\Java_study\\workspace\\myeclipse\\HomeWork\\src\\d20160624\\student.xml");
59         d4j.getDom(file);
60         d4j.showXML();
61     }
62 }

其实2种方式,都可以,都是各种包中类的方法的使用,会灵活运用就可以了!

posted @ 2016-06-24 11:46  sincoolvip  阅读(539)  评论(0编辑  收藏  举报