xml充当数据库实现电影院购票管理系统

> 创建一个movies.xml文件保存一些电影

<?xml version="1.0" encoding="UTF-8" standalone="no"?><movies>
    <movie>
        <name>黑客帝国</name>
        <director>沃卓斯基</director>
        <actor>里维斯</actor>
        <type>科幻</type>
        <price>35</price>
        <schedule>
            <item>08:00</item>
            <item>12:30</item>
        </schedule>
        <sales>0</sales>
    </movie>
    <movie>
        <name>狙击手</name>
        <director>张艺谋</director>
        <actor>章鱼</actor>
        <type>战争</type>
        <price>49</price>
        <schedule>
            <item>09:00</item>
            <item>13:30</item>
        </schedule>
        <sales>0</sales>
    </movie>
    <movie>
        <name>四海</name>
        <director>韩寒</director>
        <actor>刘昊然</actor>
        <type>喜剧</type>
        <price>60</price>
        <schedule>
            <item>10:00</item>
            <item>14:30</item>
        </schedule>
        <sales>0</sales>
    </movie>
    <movie>
        <name>误杀2</name>
        <director>戴墨</director>
        <actor>肖央</actor>
        <type>犯罪</type>
        <price>39</price>
        <schedule>
            <item>11:00</item>
            <item>15:30</item>
        </schedule>
        <sales>0</sales>
    </movie>
    <movie>
        <name>扬名立万</name>
        <director>刘循子墨</director>
        <actor>尹正</actor>
        <type>喜剧</type>
        <price>41</price>
        <schedule>
            <item>12:00</item>
            <item>16:30</item>
        </schedule>
        <sales>0</sales>
    </movie>
    <movie>
        <name>奇迹</name>
        <director>文牧野</director>
        <actor>易烊千玺</actor>
        <type>剧情</type>
        <price>43</price>
        <schedule>
            <item>08:00</item>
            <item>18:00</item>
        </schedule>
        <sales>0</sales>
    </movie>
</movies>

> 创建一个users.xml文件保存管理员和员工

<?xml version="1.0" encoding="UTF-8" standalone="no"?><users>
    <user id="admin">
        <username>tom</username>
        <password>888</password>
    </user>
    <user id="employee">
        <username>jack</username>
        <password>123</password>
    </user>
    <user id="employee">
        <username>lucy</username>
        <password>123</password>
    </user>
</users>

这里写的比较简单,并没有使用分层模式进行设计,仅用了面向对象进行了封装

>Movie类保存电影信息

package com.zhiyou.jxk.moviegosys;

import java.io.Serializable;

/**
 * @author: jxk
 * @date: 2022/1/25
 * @version: 1.0
 */
public class Movie implements Serializable {
    private String name;//电影名称
    private String director;//电影导演
    private String actor;//电影主演
    private String type;//电影类型
    private String price;//电影售价
    private String schedule;//开场时间
    private String seatNum;//电影座位
    private String ticketType;//电影票类型
    private String sales;//销量

    public String getSales() {
        return sales;
    }

    public void setSales(String sales) {
        this.sales = sales;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "name='" + name + '\'' +
                ", director='" + director + '\'' +
                ", actor='" + actor + '\'' +
                ", type='" + type + '\'' +
                ", price='" + price + '\'' +
                ", schedule='" + schedule + '\'' +
                ", seatNum='" + seatNum + '\'' +
                ", ticketType='" + ticketType + '\'' +
                '}';
    }

    public String getTicketType() {
        return ticketType;
    }

    public void setTicketType(String ticketType) {
        this.ticketType = ticketType;
    }

    public Movie(String name, String director, String actor, String type, String price, String schedule, String seatNum, String ticketType,String sales) {
        this.name = name;
        this.director = director;
        this.actor = actor;
        this.type = type;
        this.price = price;
        this.schedule = schedule;
        this.seatNum = seatNum;
        this.ticketType = ticketType;
        this.sales = sales;
    }

    public Movie(String name, String director, String actor, String type, String price, String schedule, String seatNum, String ticketType) {
        this.name = name;
        this.director = director;
        this.actor = actor;
        this.type = type;
        this.price = price;
        this.schedule = schedule;
        this.seatNum = seatNum;
        this.ticketType = ticketType;
    }

    public String getSchedule() {
        return schedule;
    }

    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }

    public String getSeatNum() {
        return seatNum;
    }

    public void setSeatNum(String seatNum) {
        this.seatNum = seatNum;
    }

    public String getName() {
        return name;
    }

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

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPrice() {
        return price;
    }

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


    public Movie() {
    }

    public Movie(String name, String director, String actor, String type, String price, String schedule, String seatNum) {
        this.name = name;
        this.director = director;
        this.actor = actor;
        this.type = type;
        this.price = price;
        this.schedule = schedule;
        this.seatNum = seatNum;
    }
}

>影院的主类

package com.zhiyou.jxk.moviegosys;

import org.w3c.dom.Element;

import java.util.Scanner;

/**
 * @author: jxk
 * @date: 2022/1/26
 * @version: 1.0
 */
public class KKCinema {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Util util = new Util();
        boolean flag = true;
        util.init();
        //先登录
        do {
            System.out.println("-------------欢 迎 使 用 KK 购 票 系 统-------------");
            System.out.println("请先登录哦~");
            //todo...写一个登录的方法
            System.out.println("请输入用户名:");
            String username = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            //登录
            System.out.println("----------");
            Element element = util.doLogin(username, password);
            if (element != null){
                //登录成功 就进行身份验证
                util.identityEnsure(element);
                //todo...根据不同的身份显示不同的功能菜单
            }else {
                System.out.println("用户名或者密码错误,登录失败。");
            }
            System.out.println("输入0返回登录,输入其他数字退出系统:");
            if (sc.nextInt() != 0){
                flag =false;
            }
        }while (flag);

    }
}

>以及最后一个工具类util

package com.zhiyou.jxk.moviegosys;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author: jxk
 * @date: 2022/1/25
 * @version: 1.0
 */
public class Util {
    private Scanner sc = new Scanner(System.in);
    private boolean flag = true;
    private int choice = 0;
    private static List<Element> userList = readXML("users.xml");//加载user元素列表
    private static List<Element> movieList = readXML("movies.xml");//加载movie元素列表
    private static Document userDoc = parseXML("users.xml");//加载用户doc
    private static Document movieDoc = parseXML("movies.xml");//加载电影doc
    //登录验证
    public Element doLogin(String username, String password) {
        //读取users.xml文件
        for (int i = 0; i < userList.size(); i++) {
            if (username.equals(userList.get(i).getElementsByTagName("username").item(0).getTextContent())
                    && password.equals(userList.get(i).getElementsByTagName("password").item(0).getTextContent())) {
                return userList.get(i);
            }
        }
        return null;
    }

    //身份验证 确认是经理还是售票员
    public void identityEnsure(Element element) {
        String id = element.getAttribute("id");
        if (id.equals("admin")) {
            //如果是经理 就调用管理端的菜单方法
            System.out.println("登录成功~\n欢迎您," + element.getElementsByTagName("username").item(0).getTextContent() + "经理~");
            showAdminMenu();
        }
        if (id.equals("employee")) {
            //如果是售票员就调用售票端的菜单方法
            System.out.println("登录成功~\n欢迎您," + element.getElementsByTagName("username").item(0).getTextContent() + "员工~");
            showEmployeeMenu(element);
        }
    }

    //显示经理的功能菜单
    public void showAdminMenu() {
        do {
            System.out.println("--------------管 理 端---------------");
            System.out.println("1. 新 增 售 票 员");
            System.out.println("2. 修 改 售 票 员");
            System.out.println("3. 删 除 售 票 员");
            System.out.println("4. 上 架 电 影");
            System.out.println("5. 下 架 电 影");
            System.out.println("6. 修 改 电 影 信 息");
            System.out.println("7. 查 看 电 影 的 售 卖 情 况");
            System.out.println("8. 退 出 登 录");
            System.out.println("请选择:");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("-->新 增 售 票 员");
                    addEmployee();
                    break;
                case 2:
                    System.out.println("-->修 改 售 票 员");
                    updateEmployee();
                    break;
                case 3:
                    System.out.println("-->删 除 售 票 员");
                    deleteEmployee();
                    break;
                case 4:
                    System.out.println("-->上 架 电 影");
                    addMovie();
                    break;
                case 5:
                    System.out.println("-->下 架 电 影");
                    delMovie();
                    break;
                case 6:
                    System.out.println("-->修 改 电 影 信 息");
                    updateMovie();
                    break;
                case 7:
                    System.out.println("-->查 看 电 影 的 售 卖 情 况");
                    showSales();
                    break;
                case 8:
                    System.out.println("-->退 出 登 录");
                    flag = false;
            }
        } while (flag);

    }
    //查看电影卖出的情况
    private void showSales() {
        List<Element> elementList = readXML("movies.xml");
        System.out.println("电影名称\t售出数量");
        for (Element e: elementList) {
            System.out.println(e.getElementsByTagName("name").item(0).getTextContent()+"\t"+
                    e.getElementsByTagName("sales").item(0).getTextContent());
        }
    }

    //todo ...修改电影信息
    public void updateMovie() {
        //只修改票价
        System.out.println("请输入电影的名称:");
        String name = sc.next();
        Element e = null;
        if ((e = isExist3(name,movieDoc)) == null){
            System.out.println("该电影不存在,修改失败");
        }else {
            System.out.println("请输入修改后的票价:");
            String price = sc.next();
            e.getElementsByTagName("price").item(0).setTextContent(price);
            safeXML(movieDoc,"movies.xml");
            System.out.println("修改成功");
        }
    }

    //todo...下架电影
    public void delMovie() {
        System.out.println("请输入要下架的电影的名称:");
        String delName = sc.next();
        Element e = null;
        if ((e = isExist3(delName,movieDoc)) != null){
            e.getParentNode().removeChild(e);
        }else{
            System.out.println("该电影不存在,下架失败");
        }
        safeXML(movieDoc,"movies.xml");
        System.out.println("下架成功");
    }

    //todo...上架电影
    public void addMovie() {
        System.out.println("请输入要上架的电影名称:");
        String name = sc.next();
        Element e = null;
        if ((e = isExist3(name,movieDoc)) != null){
            System.out.println("该电影已存在,请勿重复上架");
        }else{
            System.out.println("请输入电影的导演:");
            String director = sc.next();
            System.out.println("请输入电影的主演:");
            String actor = sc.next();
            System.out.println("请输入电影的类型:");
            String type = sc.next();
            System.out.println("请输入票价:");
            String price = sc.next();
            System.out.println("请输入第一个场次时间:");
            String firstTime = sc.next();
            System.out.println("请输入第二个场次时间:");
            String secondTime = sc.next();
            Element movie = movieDoc.createElement("movie");
            Element name1 = movieDoc.createElement("name");
            Element director1 = movieDoc.createElement("director");
            Element actor1 = movieDoc.createElement("actor");
            Element type1 = movieDoc.createElement("type");
            Element price1 = movieDoc.createElement("price");
            Element schedule = movieDoc.createElement("schedule");
            Element sales = movieDoc.createElement("sales");
            Element firstItem = movieDoc.createElement("item");
            Element secondItem = movieDoc.createElement("item");
            name1.setTextContent(name);
            director1.setTextContent(director);
            actor1.setTextContent(actor);
            type1.setTextContent(type);
            price1.setTextContent(price);
            sales.setTextContent("0");
            firstItem.setTextContent(firstTime);
            secondItem.setTextContent(secondTime);

            movie.appendChild(name1);
            movie.appendChild(director1);
            movie.appendChild(actor1);
            movie.appendChild(type1);
            movie.appendChild(price1);
            movie.appendChild(schedule);
            movie.appendChild(sales);
            schedule.appendChild(firstItem);
            schedule.appendChild(secondItem);
            movieDoc.getChildNodes().item(0).appendChild(movie);
            safeXML(movieDoc,"movies.xml");
            System.out.println("上架成功");
        }
    }

    //读取xml文件
    public List<Element> readXML2(Document doc) {
        ArrayList<Element> list = new ArrayList<>();
        try {
            Node root = doc.getChildNodes().item(0);
            NodeList nodes = root.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node item = nodes.item(i);
                if (item.getNodeType() == 1) {
                    Element e = (Element) item;
                    list.add(e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    //todo...修改售票员信息 注意必须是对同一个doc文档修改
    public void updateEmployee() {

        System.out.println("请输入要修改的员工的姓名:");
        String name = sc.next();
        Element e = null;
        if ((e = isExist2(name,userDoc)) !=null){
            System.out.println(e.getTextContent());
            //存在的话修改
            System.out.println("是否修改姓名:(y/n)");
            if (sc.next().equals("y")){
                System.out.println("请输入修改后的姓名:");
                String username = sc.next();
                if (!isExist(username)){
                    e.getElementsByTagName("username").item(0).setTextContent(username);
                }else {
                    System.out.println("该名字已存在,修改失败");
                }
            }
            System.out.println("是否修改密码:(y/n)");
            if (sc.next().equals("y")){
                System.out.println("请输入修改后的密码:");
                e.getElementsByTagName("password").item(0).setTextContent(sc.next());
            }

        }else{
            //不存在则提示
            System.out.println("该员工不存在,修改失败");
        }
        safeXML(userDoc,"users.xml");
        System.out.println("修改成功");
    }

    //todo...删除售票员
    public void deleteEmployee() {
        //思路:找到要删除的user的父节点 然后移除即可
        System.out.println("请输入要删除的员工的姓名:");
        String delName = sc.next();
        Element e = null;
        if ((e = isExist2(delName,userDoc)) != null){
            //如果找到了就找父节点
            e.getParentNode().removeChild(e);
        }else {
            System.out.println("该员工不存在,删除失败");
        }
        safeXML(userDoc,"users.xml");
        System.out.println("删除成功");
    }

    //新增售票员
    public void addEmployee() {
        //解析users.xml文件
        System.out.println("请输入员工的姓名:");
        String name = sc.next();
        if (isExist(name)) {
            System.out.println("该员工已存在,新增失败");
            return;
        }
        System.out.println("请输入员工的密码:");
        String password = sc.next();
        Element userElement = userDoc.createElement("user");
        userElement.setAttribute("id", "employee");

        Element username = userDoc.createElement("username");
        username.setTextContent(name);
        userElement.appendChild(username);

        Element pwd = userDoc.createElement("password");
        pwd.setTextContent(password);
        userElement.appendChild(pwd);
        userDoc.getChildNodes().item(0).appendChild(userElement);
        safeXML(userDoc, "users.xml");
        System.out.println("增加成功");
    }



    //判断该名字是否存在
    public boolean isExist(String name) {
        //拿到user的element
        List<Element> elementList = readXML("users.xml");
        for (Element e : elementList) {
            if (e.getElementsByTagName("username").item(0).getTextContent().equals(name)) {
                return true;
            }
        }
        return false;
    }
    //判断该名字是否存在
    public Element isExist2(String name,Document document) {
        //拿到user的element
        List<Element> elementList = readXML2(document);
        for (Element e : elementList) {
            if (e.getElementsByTagName("username").item(0).getTextContent().equals(name)) {
                return e;
            }
        }
        return null;
    }

    //判断电影名字是否存在
    public Element isExist3(String name,Document document) {
        //拿到user的element
        List<Element> elementList = readXML2(document);
        for (Element e : elementList) {
            if (e.getElementsByTagName("name").item(0).getTextContent().equals(name)) {
                return e;
            }
        }
        return null;
    }

    //显示售票员的功能菜单
    public void showEmployeeMenu(Element element) {
        do {
            System.out.println("--------------雇 员 端---------------");
            System.out.println("1. 查 看 所 有 电 影");
            System.out.println("2. 购 票 服 务");
            System.out.println("8. 退 出 登 录");
            System.out.println("请选择:");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("-->查 看 所 有 电 影");
                    showAllMovie();
                    break;
                case 2:
                    System.out.println("-->购 票 服 务");
                    buyTickets();
                    break;
                case 8:
                    System.out.println("-->退 出 登 录");
                    flag = false;
            }
        } while (flag);
    }

    //购买电影票
    public void buyTickets() {
        //确定电影及座位
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入电影名称:");
        String name = sc.next();
        Element e = checkMovie(name);
        if (e == null) {
            System.out.println("此电影不存在");
            return;
        }
        System.out.println("请输入时间:");
        String schedule = sc.next();
        if (!checkTime(e, schedule)) {
            System.out.println("日期错误");
            return;
        }
        System.out.println("请输入您所要购买的票的类型: 1.普通票   2.学生票   3.赠送票");
        String type = sc.next();
        System.out.println("请输入您所需要的座位号: 以排-列的形式");
        String seatNum = sc.next();
        if (!checkSeatNum(seatNum)) {
            System.out.println("座位号不存在");
            return;
        }
        Movie movie = new Movie(name, e.getElementsByTagName("director").item(0).getTextContent(),
                e.getElementsByTagName("actor").item(0).getTextContent(),
                e.getElementsByTagName("type").item(0).getTextContent(),
                e.getElementsByTagName("price").item(0).getTextContent(),
                schedule, seatNum,type);
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        List<Movie> existMovies = null;

        try {
            ois = new ObjectInputStream(new FileInputStream("tickets.txt"));
            existMovies = (List<Movie>) ois.readObject();
            for (Movie m : existMovies) {
                if (m.getName().equals(name) && m.getSchedule().equals(schedule) && m.getSeatNum().equals(seatNum)) {
                    System.out.println("此票已售出,购买失败");
                    return;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }

        try {
            existMovies.add(movie);
            oos = new ObjectOutputStream(new FileOutputStream("tickets.txt"));
            oos.writeObject(existMovies);
            //todo...打印小票
            receiptPrint(movie);
            System.out.println("购买成功!");
            count(name);//购买成功就+1
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }


    }

    private void count(String name) {
        List<Element> elementList = readXML2(movieDoc);
        int counts = 0;
        for (Element e: elementList) {
            if (e.getElementsByTagName("name").item(0).getTextContent().equals(name)){
                String sales = e.getElementsByTagName("sales").item(0).getTextContent();
                counts = Integer.parseInt(sales) + 1;
                e.getElementsByTagName("sales").item(0).setTextContent(String.valueOf(counts));
            }
        }
        safeXML(movieDoc,"movies.xml");
    }

    //初始化
    public void init() {
        File file = new File("tickets.txt");
        ObjectOutputStream oos = null;

        if (!file.exists()) {
            try {
                file.createNewFile();
                List<Movie> list = new ArrayList<>();
                oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(list);
            } catch (IOException exception) {
                exception.printStackTrace();
            } finally {
                try {
                    oos.close();
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    //打印小票
    public void receiptPrint(Movie movie) {
        //反序列化 读取文件 然后写入到txt文件中
        BufferedWriter bw = null;
        //打印小票
        String hours = movie.getSchedule().substring(0, 2) + "时";
        String minutes = movie.getSchedule().substring(3, 5) + "分";
        String path = movie.getName() + " " + movie.getSeatNum() + " " + hours + minutes + ".txt";
        try {
            bw = new BufferedWriter(new FileWriter(new File(path)));
            bw.write("********************");
            bw.newLine();
            bw.write("        KK影院       ");
            bw.newLine();
            bw.write("--------------------");
            bw.newLine();
            bw.write(" 电影名: " + movie.getName());
            bw.newLine();
            bw.write(" 时间: " + movie.getSchedule());
            bw.newLine();
            bw.write(" 座位号: " + movie.getSeatNum());
            bw.newLine();
            bw.write(" 价格: " + movie.getPrice());
            bw.newLine();
            bw.write("**********************");
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    }
    //判断时间
    public boolean checkTime(Element e, String schedule) {
        NodeList item = e.getElementsByTagName("item");
        for (int i = 0; i < item.getLength(); i++) {
            if (schedule.equals(item.item(i).getTextContent())) {
                return true;
            }
        }
        return false;
    }

    //判断座位号的格式是否正确
    public boolean checkSeatNum(String seatNum) {
        String regex = "[1-5]-[1-7]";
        return seatNum.matches(regex);
    }

    public Element checkMovie(String name) {
        //先拿到电影的数组
        List<Element> elementList = readXML("movies.xml");
        //遍历循环
        for (Element e : elementList) {
            if (name.equals(e.getElementsByTagName("name").item(0).getTextContent())) {
                return e;
            }
        }
        return null;
    }

    public void showMap() {
        System.out.println("以下为影院的座位结构图:");
        //显示座位
        System.out.println("------------------------荧幕------------------------");
        System.out.println();

        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 8; j++) {
                String seatNum = i + "-" + j;
                System.out.print(seatNum + "\t  ");
            }
            System.out.println();
        }
    }

    //查看所有电影
    public void showAllMovie() {

        int count = 1;
        List<Element> elementList = readXML("movies.xml");
        System.out.println("---------->电影信息如下:");
        System.out.println("序号\t电影名称\t导演\t演员\t类型\t价格\t时间");
        for (int i = 0; i < elementList.size(); i++) {
            Element e = elementList.get(i);
            int length = e.getElementsByTagName("item").getLength();
            for (int j = 0; j < length; j++) {
                System.out.print(count + "\t" + e.getElementsByTagName("name").item(0).getTextContent() + "\t" +
                        e.getElementsByTagName("director").item(0).getTextContent() + "\t" +
                        e.getElementsByTagName("actor").item(0).getTextContent() + "\t" +
                        e.getElementsByTagName("type").item(0).getTextContent() + "\t" +
                        e.getElementsByTagName("price").item(0).getTextContent() + "\t" +
                        e.getElementsByTagName("item").item(j).getTextContent() + "\n");
                count++;
            }
        }
        //显示电影院座位图
        showMap();
    }

    //将xml文件解析成一颗倒立的树
    public static Document parseXML(String path) {
        Document doc = null;
        try {
            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doc;
    }

    //把更新后的内容保存下来
    public void safeXML(Document doc, String path) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource domSource = new DOMSource(doc);
            //设置编码类型
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            StreamResult result = new StreamResult(new FileOutputStream(path));
            //把dom树转换成xml文件
            transformer.transform(domSource, result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //读取xml文件
    public static List<Element> readXML(String path) {
        ArrayList<Element> list = new ArrayList<>();
        try {
            Document doc = parseXML(path);
            Node root = doc.getChildNodes().item(0);
            NodeList nodes = root.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node item = nodes.item(i);
                if (item.getNodeType() == 1) {
                    Element e = (Element) item;
                    list.add(e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值