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;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值