XML解析

package cm.luo.test;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class test {
        public static void main(String[] args) throws Exception {

            List<student> list = new ArrayList<student>();

            File f = new File("src/student.xml");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(f);
            Element emRoot = doc.getRootElement();// 根节点(Students)
            Iterator<Element> it = emRoot.elementIterator();// 遍历Students下面的子节点
            while (it.hasNext()) {
                Element emStudent = it.next();
                Attribute attr = emStudent.attribute("id");
                // System.out.println(attr.getValue());
                String id = attr.getValue();// 得到id属性值
                student stu = new student();
                stu.setId(id);
                Iterator<Element> emEnd = emStudent.elementIterator();// 遍历Student下面的子节点
                while (emEnd.hasNext()) {
                    Element em = emEnd.next();
                    if ("name".equals(em.getName())) {
                        // System.out.println("name==" + em.getText());
                        stu.setName(em.getText());
                    }
                    if ("course".equals(em.getName())) {
                        // System.out.println("course==" + em.getText());
                        stu.setCourse(em.getText());
                    }
                    if ("score".equals(em.getName())) {
                        // System.out.println("score==" + em.getText());
                        stu.setScore(Double.parseDouble(em.getText()));
                    }
                }
                list.add(stu);

            }

            for (student stu : list) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getCourse() + "\t" + stu.getScore());
            }

        }

    }
<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student id="stu001">
        <name>张三</name>
        <course>java</course>
        <score>93</score>
    </student>
    <student id="stu002">
        <name>李四</name>
        <course>oracle</course>
        <score>98</score>
    </student>
</students>
package cm.luo.test;

public class student {
    private String id;
    private String name;
    private String course;
    private double score;


    public student(){}


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }


}

版权声明:本文为for_luo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。