javaWeb-day01(XML练习案例)

这篇博客介绍了如何使用JavaWeb进行XML文件的学生信息管理,包括创建Javabean、实现添加、查询和删除功能。开发过程遵循从底层到上层的结构,将不同模块的代码组织在不同包内,如domain包存储数据实体,dao包实现数据操作。重点强调了使用equals方法比较字符串,以及在工具类中封装XML文档操作。
摘要由CSDN通过智能技术生成

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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<exam>
	<student idcard="111" examid="222">
		<name>张三</name>
		<location>沈阳</location>
		<grade>89</grade>
	</student>
	<student idcard="333" examid="444">
		<name>李四</name>
		<location>大连</location>
		<grade>97</grade>
	</student>
</exam>


2、编程实现如下功能



3、实现学生信息的添加



4、实现学生信息的查询



5、实现学生的删除功能




模块:



强调几点:

  1. 先写Javabean,做开发是从底层开发到上层。Javabean ---> dao --->  界面。
  2. 不同模块的程序放在不同的包里面,这样看到包的名字就知道是什么模块。
  3. 数据实体,放到一个叫 domain 的包里面。封装 Student 对象。
  4. 数据库里面存放什么数据,就在 Student 对象里封装什么属性。再生成 get、set 方法。
  5. 再写dao,你这个软件给用户提供些什么方法,就在dao里实现什么方法。void add(Student s)、void delete(String name)、Student find(String examid)。
  6. 根据用户的需求来设置参数,用户希望通过什么来查找,你就设置什么参数。
  7. 这三个方法都有共同的代码,都要将XML文档封装成 Document 对象,都要将数据存储到XML文档中去。抽取出来封装到工具类里。放在工具包里。
  8. 建 utils 包来放工具类。放 XmlUtils 类。工具类的所有方法都是静态的。public static Document getDocument()、public static void write2Xml(Document document)。
  9. to 要简写成 2 (到,至 的意思)、for 要简写成 4 (给 的意思)。
  10. 界面层打包到 view 包里。Main 是有 main 函数的可运行程序。new BufferedReader(new InputStreamReader(System.in));   String type  = br.readLine();


String类

 booleanequalsIgnoreCase(String anotherString) 
          将此 String 与另一个 String 比较,不考虑大小写。

!注意:

比较两个字符串一定要用 equals 方法!


Double类

static doubleparseDouble(String s) 
          返回一个新的 double 值,该值被初始化为用指定 String 表示的值,这与 Double 类的 valueOf 方法一样。


代码:



student.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<exam>
    <student examid="222" idcard="111">
        <name>张三</name>
        <location>沈阳</location>
        <grade>89</grade>
    </student>
    <student examid="111" idcard="123">
        <name>黎明</name>
        <location>上海</location>
        <grade>98.5</grade>
    </student>
</exam>


Student.java

package cn.itcast.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.itcast.utils;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class XmlUtils {

	public static Document getDocument() throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(new File("src/student.xml"));
		return document;
	}
	
	public static void write2Xml(Document document) throws Exception{
		TransformerFactory fac = TransformerFactory.newInstance();
		Transformer tf = fac.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File("src/student.xml")));
	}
}


StudentDao.java
package cn.itcast.dao;

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

import cn.itcast.domain.Student;
import cn.itcast.utils.XmlUtils;

public class StudentDao {
	
	public void add(Student student){
		try {
			Document document = XmlUtils.getDocument();
			
			Element student_node = document.createElement("student");
			student_node.setAttribute("idcard", student.getIdcard());
			student_node.setAttribute("examid", student.getExamid());
			
			Element name = document.createElement("name");
			name.setTextContent(student.getName());
			
			Element location = document.createElement("location");
			location.setTextContent(student.getLocation());
			
			Element grade = document.createElement("grade");
			grade.setTextContent(student.getGrade()+"");
			
			student_node.appendChild(name);
			student_node.appendChild(location);
			student_node.appendChild(grade);
			
			Node exam = document.getElementsByTagName("exam").item(0);
			exam.appendChild(student_node);
			
			XmlUtils.write2Xml(document);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public void delete(String name){
		try {
			Document document = XmlUtils.getDocument();
			NodeList nameList = document.getElementsByTagName("name");
			for(int x=0; x<nameList.getLength(); x++){
				Node nameNode = nameList.item(x);
				if(nameNode.getTextContent().equals(name)){
					Node student = nameNode.getParentNode();
					student.getParentNode().removeChild(student);
					XmlUtils.write2Xml(document);
					return;
				}
			}
			throw new RuntimeException("对不起,您要删除的学生不存在!"); //异常也是一种返回值
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public Student find(String examid){
		try {
			Document document = XmlUtils.getDocument();
			NodeList studentList = document.getElementsByTagName("student");
			for(int x=0; x<studentList.getLength(); x++){
				Element studentEle = (Element)studentList.item(x);
				if(studentEle.getAttribute("examid").equals(examid)){
					String idcard = studentEle.getAttribute("idcard");
					String name = studentEle.getElementsByTagName("name").item(0).getTextContent();
					String location = studentEle.getElementsByTagName("location").item(0).getTextContent();
					String grade = studentEle.getElementsByTagName("grade").item(0).getTextContent();
					
					Student student = new Student();
					student.setExamid(examid);
					student.setIdcard(idcard);
					student.setName(name);
					student.setLocation(location);
					student.setGrade(Double.parseDouble(grade));
					return student;
				}
			}
			return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}


Main.java
package cn.itcast.view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;

public class Main {

	public static void main(String[] args) {
		System.out.println("添加用户:(a)  删除用户:(b)  查询成绩:(c)");
		System.out.print("请输入操作类型:");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String type = br.readLine();
			if(type.equalsIgnoreCase("a")){
				try {
					System.out.print("请输入学生姓名:");
					String name = br.readLine();
					System.out.print("请输入学生准考证号:");
					String examid = br.readLine();
					System.out.print("请输入学生身份证号:");
					String idcard = br.readLine();
					System.out.print("请输入学生所在地:");
					String location = br.readLine();
					System.out.print("请输入学生成绩:");
					Double grade = Double.parseDouble(br.readLine());
					
					Student student = new Student();
					student.setName(name);
					student.setExamid(examid);
					student.setIdcard(idcard);
					student.setLocation(location);
					student.setGrade(grade);
					
					StudentDao sd = new StudentDao();
					sd.add(student);
					
					System.out.println("恭喜您,添加成功!");
				} catch (Exception e) {
					System.out.println("对不起,添加失败!");
				}
			}else if(type.equalsIgnoreCase("b")){
				try {
					System.out.print("请输入学生姓名:");
					String name = br.readLine();
					StudentDao sd = new StudentDao();
					sd.delete(name);
					System.out.println("成功删除!");
				} catch (Exception e) {
					System.out.println("对不起,您要删除的学生不存在!");
				}
			}else if(type.equalsIgnoreCase("c")){
				try {
					System.out.print("请输入学生准考证号:");
					String examid = br.readLine();
					StudentDao sd = new StudentDao();
					Student student = sd.find(examid);
					if(student==null){
						System.out.println("您查询的学生不存在!");
					}else{
						System.out.println("您查询的学生信息为:");
						System.out.println("姓名:"+student.getName()+
								"  身份证号:"+student.getIdcard()+
								"  所在地:"+student.getLocation()+
								"  成绩:"+student.getGrade());
					}
				} catch (Exception e) {
					System.out.println("对不起,查找失败!");
				}
				
			}else{
				System.out.println("不支持此操作!");
			}
			
		} catch (IOException e) {
			System.out.println("对不起,操作失败!");
		}

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值