javaweb-day01-7(XML 解析-案例)

2024-09-07 16:08
文章标签 java xml web 解析 案例 day01

本文主要是介绍javaweb-day01-7(XML 解析-案例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、以如下格式的exam.xml文件为例

exam.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam><student examid="222" idcard="111"><name>张三</name><location>沈阳</location><grade>89.00</grade></student><student examid="444" idcard="333"><name>李四</name><location>大连</location><grade>97.00</grade></student></exam>



2、编程实现如下功能

 

3、实现学生信息的添加

 

4、实现学生信息的查询

 

5、实现学生的删除功能

 

 

添加数据:


查询数据:


 

开发顺序:先写javabean,再写Dao用到的工具类、Dao ,最后写界面层Main。

不同模块的程序,要放在不同的包里:

  •   实体javabean:放在domain包。
  •   Dao:放在dao包。dao操作xml文档必定会有重复代码:得到document、更新xml文档。将他们抽取到工具类XmlUtils里。
  •   工具类XmlUtils:放在utils工具包中。
  •   界面Main:放在main包。

 

 

Student.java

package cn.mengmei.domain;public class Student {private String idcard;private String examid;private String name;private String location;private Double grade;public String getIdcard() {return idcard;}public void setIdcard(String idcard) {this.idcard = idcard;}public String getExamid() {return examid;}public void setExamid(String examid) {this.examid = examid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}public Double getGrade() {return grade;}public void setGrade(Double grade) {this.grade = grade;}}



XMLUtils.java

package cn.mengmei.utils;import java.io.IOException;import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;
import org.xml.sax.SAXException;public class XMLUtils {public static Document document() throws SAXException, IOException, ParserConfigurationException  {Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("src/exam.xml");return document;}public static void updateXML(Document document) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError  {TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult("src/exam.xml"));}
}


 

StudentDao.java

package cn.mengmei.dao;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;import cn.mengmei.domain.Student;
import cn.mengmei.utils.XMLUtils;public class StudentDao {public void add(Student student) {try {Document document = XMLUtils.document();Element stuNode = document.createElement("student");stuNode.setAttribute("idcard", student.getIdcard());stuNode.setAttribute("examid", student.getExamid());Element nameNode = document.createElement("name");nameNode.setTextContent(student.getName());Element locNode = document.createElement("location");locNode.setTextContent(student.getLocation());Element graNode = document.createElement("grade");graNode.setTextContent(student.getGrade()+"");stuNode.appendChild(nameNode);stuNode.appendChild(locNode);stuNode.appendChild(graNode);document.getDocumentElement().appendChild(stuNode);XMLUtils.updateXML(document);} catch (Exception e) {throw new RuntimeException(e);}}public void delete(String name){try {Document document = XMLUtils.document();NodeList nameList = document.getElementsByTagName("name");for(int i=0;i<nameList.getLength();i++){Node nameNode = nameList.item(i);if(nameNode.getTextContent().equals(name)){Node stuNode = nameNode.getParentNode();stuNode.getParentNode().removeChild(stuNode);XMLUtils.updateXML(document);return;}}throw new RuntimeException("-------对不起,你要删除的学生信息不存在!------");  //异常也是一种返回值。} catch (Exception e) {throw new RuntimeException(e);}}public Student find(String examid){Document document = null;try {document = XMLUtils.document();NodeList stuList = document.getElementsByTagName("student");Student student = null;for(int i=0;i<stuList.getLength();i++){Element stuNode = (Element)stuList.item(i);if(Integer.parseInt(stuNode.getAttribute("examid")) == Integer.parseInt(examid)){student = new Student();student.setName(stuNode.getElementsByTagName("name").item(0).getTextContent());student.setIdcard(stuNode.getAttribute("idcard"));student.setExamid(stuNode.getAttribute("examid"));student.setLocation(stuNode.getElementsByTagName("location").item(0).getTextContent());student.setGrade(Double.parseDouble(stuNode.getElementsByTagName("grade").item(0).getTextContent()));}}return student;} catch (Exception e) {throw new RuntimeException(e);}}}



Main.java

package cn.mengmei.main;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;import cn.mengmei.dao.StudentDao;
import cn.mengmei.domain.Student;public class Main {public static void main(String[] args) {System.out.println("添加用户:(a)    删除用户:(b)    查询成绩:(c)");System.out.print("请输入操作类型:");BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));String item = null;try {item = reader.readLine();} catch (IOException e1) {System.out.println("--------对不起,读取类型失败!--------");}String name = null;String idcard = null;String examid = null;String location = null;String grade = null;Student student = null;StudentDao dao = null;switch (item) {case "a":try {System.out.print("请输入学生姓名:");name = reader.readLine();System.out.print("请输入学生身份证号:");idcard = reader.readLine();System.out.print("请输入学生准考证号:");examid = reader.readLine();System.out.print("请输入学生所在地:");location = reader.readLine();System.out.print("请输入学生成绩:");grade = reader.readLine(); student = new Student();student.setName(name);student.setIdcard(idcard);student.setExamid(examid);student.setLocation(location);student.setGrade(Double.parseDouble(grade));dao = new StudentDao();dao.add(student);System.out.println("-----------添加数据成功-----------");} catch (Exception e) {System.out.println("-----------对不起,添加数据失败!-----------");}break;case "b":try {System.out.print("请输入要删除的学生姓名:");name = reader.readLine();dao = new StudentDao();dao.delete(name);System.out.println("------对已成功删除学生信息------");} catch (Exception e) {System.out.println(e.getMessage());}break;case "c":try {System.out.print("请输入要查询的学生准考证号:");examid = reader.readLine();dao = new StudentDao();student = dao.find(examid);if(student!=null){System.out.println("姓名:"+student.getName());System.out.println("身份证号:"+student.getIdcard());System.out.println("准考证号:"+student.getExamid());System.out.println("所在地:"+student.getLocation());System.out.println("成绩:"+student.getGrade());}else{System.out.println("--------对不起,没有查询到此学生!-------");}} catch (Exception e) {System.out.println("------对不起,查询失败!------");}break;default:System.out.println("-------非法类型-------");break;}}}



 

 

这篇关于javaweb-day01-7(XML 解析-案例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145525

相关文章

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2