会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132389个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 3799楼
JAVA 全系列/第七阶段:生产环境部署与协同开发/Docker 3800楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 3801楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 3803楼
JAVA 全系列/第八阶段:Linux入门到实战/Linux(旧) 3804楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 3805楼

image.png

老师,为什么会报错?我明明实例化了Book对象。

package com.bjsxt.dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.awt.print.Book;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestDOM4J {
    public static void main(String[] args) throws DocumentException {
        //(1) 创建SAXReader对象
        SAXReader reader = new SAXReader();
        //(2) 调用read方法
        Document doc = reader.read(new File("book.xml"));
        //(3) 获取根元素
        Element root = doc.getRootElement(); //books
        //(4) 通过迭代器遍历直接节点
        for (Iterator<Element> iteBook = root.elementIterator(); iteBook.hasNext();){
            Element bookEle = iteBook.next();
            //得到book的属性
            for (Iterator<Attribute> iteAtt= bookEle.attributeIterator(); iteAtt.hasNext();){
                Attribute att = iteAtt.next();
                System.out.println(att.getName()+"\t"+att.getText());
            }

        }
        System.out.println("----------------------------------------");
        List<Book> bookList = new ArrayList<>();
        for (Iterator<Element> iteBook=root.elementIterator();iteBook.hasNext();){
            //创建一个Book对象
            Book book = new Book();
            Element bookEle = iteBook.next(); //得到每一个book
            //使用for循环继续遍历
            for (Iterator<Element> subBookEle = bookEle.elementIterator();subBookEle.hasNext();){
                //得到每一个子元素
                Element subEle = subBookEle.next();
                System.out.println(subEle.getName()+"\t"+subEle.getText());
                /*
                * 分装成Book对象
                * */
                //获取节点的名称
                String nodeName = subEle.getName();   //name,author,price
                //使用switch判断
                switch (nodeName){
                    case "name":
                        book.setName(subEle.getText());
                        break;
                    case "author":
                        book.setAuthor(subEle.getText());
                        break;
                    case "price":
                        book.setPrice(Double.parseDouble(subEle.getText()));
                        break;
                }

            }
            //添加集合中
            bookList.add(book);
        }
        //遍历集合
        System.out.println("\n遍历集合-----------------------\n");
        for (Book b: bookList){
            System.out.println(b.getName()+"\t"+b.getAuthor()+"\t"+b.getPrice());
        }
    }
}
package com.bjsxt.entity;

public class Book {
    //私有属性
    private String name;
    private String author;
    private double price;

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book() {
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3806楼
JAVA 全系列/(隐藏)第二十三阶段:数字货币交易所项目/服务中台_定时任务 3807楼
人工智能/第七阶段:机器学习-无监督学习/KMeans聚类 3808楼
WEB前端全系列/第十一阶段:Nodejs编程模块--/Nodejs基础 3809楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637