【2-3作业】在Java中保留xml数据的结构

来源:2-3 使用 DOM 解析XML 文件的节点名和节点值

都是已存在

2022-03-05 16:21

//Book类
package com.imooc.xmlText;

public class Book {
    private String id;
    private String name;
    private String author;
    private String year;
    private String price;
    private String language;

    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 getAuthor() {
        return author;
    }

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

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPrice() {
        return price;
    }

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

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}


////////////////////////////////////////////////////////////
package com.imooc.xmlText;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DOMTest1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
        //解析xml文件
        Document document=documentBuilder.parse("demo/books1.xml");
        NodeList bookList= document.getElementsByTagName("book");
        Book newBook=new Book();
        List<Book> newBookList=new ArrayList<Book>();
        System.out.println("共有"+bookList.getLength()+"本书");
        for(int i=0;i<bookList.getLength();i++){
            Node book=bookList.item(i);
            NamedNodeMap attrs= book.getAttributes();
            for(int j=0;j<attrs.getLength();j++){
                Node attr=attrs.item(j);
                newBook.setId(attr.getNodeValue());
                System.out.println(attr.getNodeName()+":"+attr.getNodeValue());}
            NodeList childList=book.getChildNodes();
            for(int m=0;m<childList.getLength();m++){
                Node child=childList.item(m);
                if(child.getNodeType()==Node.ELEMENT_NODE){
                    switch(child.getNodeName()){
                        case "name":
                            newBook.setName(child.getTextContent());
                            break;
                        case "author":
                            newBook.setAuthor(child.getTextContent());
                            break;
                        case "year":
                            newBook.setYear(child.getTextContent());
                            break;
                        case "price":
                            newBook.setPrice(child.getTextContent());
                            break;
                        case "language":
                            newBook.setLanguage(child.getTextContent());
                            break;
                    }
                    System.out.println(child.getNodeName()+":"+child.getTextContent());}
            }
            newBookList.add(newBook);
            newBook=new Book();
        }
        System.out.println("共有"+newBookList.size()+"本书");
        for (Book book:newBookList) {
            System.out.println("第"+book.getId()+"本书");
            System.out.println("Name:"+book.getName());
            System.out.println("Author:"+book.getAuthor());
            System.out.println("Year:"+book.getYear());
            System.out.println("Price:"+book.getPrice());
            System.out.println("Language:"+book.getLanguage());
            System.out.println("----finish----");
        }


写回答 关注

1回答

  • 慕梦前来
    2022-07-05 20:02:20

    这个写法可以,很有规范

Java眼中的XML---文件读取

通过Java认识并且创造XML文件,如何应用 Java“解析 XML

83199 学习 · 431 问题

查看课程

相似问题