XML管理实例

前端之家收集整理的这篇文章主要介绍了XML管理实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML中添加删除、查找学生对象

public class main {
	public static void main(String[] args) {
		try{
		System.out.println("添加学生(a)   删除学生(b)   查找学生(c)");
		System.out.println("请输入操作类型");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String type = br.readLine();
		switch(type)
		{
		case "a":
			System.out.println("请输入学生姓名:");
			String name = br.readLine();
			
			System.out.println("请输入学生姓准考证好:");
			String examid = br.readLine();
			
			System.out.println("请输入学生身份证号:");
			String idcard = br.readLine();
			
			System.out.println("请输入学生所在地:");
			String location = br.readLine();
			
			System.out.println("请输入学生成绩:");
			String grade = br.readLine();
			Student s = new Student();
			s.setExam(examid);
			s.setName(name);
			s.setGrade(Double.parseDouble(grade));
			s.setIdcard(idcard);
			s.setLocation(location);
			
			StudentDao dao = new StudentDao();
			dao.add(s);
			
			System.out.println("添加成功");
			break;
		case "b":
			System.out.println("请输入要删除的学生的姓名:");
			String nameB = br.readLine();
			
			try{
			StudentDao daoB = new StudentDao();
			daoB.delete(nameB);
			System.out.println("删除成功!");
			}catch(StudentNotExistException e)
			{
				System.out.println("学生名不存在");
			}
			break;
		case "c":
			Student sc =null;
			System.out.println("请输入要删除的学生的准考证号:");
			String examidC = br.readLine();
			StudentDao daoC = new StudentDao();
			if((sc = daoC.find(examidC))!=null)
			{
				System.out.println(sc.toString());
			}else{
				System.out.println("没有该学生");
			}
			break;
		default:
			System.out.println("不支持!");
		}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("出错!");
		}



学生类

public class Student {

	private String idcard;
	private String exam;
	private String name;
	private String location;
	private double grade;
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	public String getExam() {
		return exam;
	}
	public void setExam(String exam) {
		this.exam = exam;
	}
	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;
	}
}


添加删除、查找 的方法

public class StudentDao {

	public void add(Student s){
		try {
			Document document = XmlUtils.getDocument();
			//创建出封装学生信息的标签
			Element student_tag = document.createElement("Student");
			student_tag.setAttribute("idcard",s.getIdcard());
			student_tag.setAttribute("examid",s.getExam());
			
			//创建用于封装学生姓名、所在地和成绩标签
			Element name = document.createElement("name");
			Element location = document.createElement("location");
			Element grade = document.createElement("grade");
			
			name.setTextContent(s.getName());
			location.setTextContent(s.getLocation());
			grade.setTextContent(s.getGrade()+"");
			
			student_tag.appendChild(name);
			student_tag.appendChild(location);
			student_tag.appendChild(grade);
			
			//把封装了信息学生标签挂到文档上
			document.getElementsByTagName("exam").item(0).appendChild(student_tag);
			
			//更新内存
			XmlUtils.write2Xml(document);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	public Student find(String examid){
		try {
			Document document = XmlUtils.getDocument();
			
			NodeList list = document.getElementsByTagName("Student");
			for(int i=0;i<list.getLength();i++)
			{
				Element student_tag = (Element) list.item(i);
				if(student_tag.getAttribute("examid").equals(examid)){
					//找到与examid向匹配的学生,new一个student对象封装这个学生的信息返回
					Student s = new Student();
					s.setExam(examid);
					s.setIdcard(student_tag.getAttribute("idcard"));
					s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
					s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
					s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
					return s;
				}
			}
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	public void delete(String name) throws StudentNotExistException{
		try {
			Document document = XmlUtils.getDocument();
			NodeList list = document.getElementsByTagName("name");
			for(int i=0;i<list.getLength();i++){
				if(list.item(i).getTextContent().equals(name)){
					list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
					XmlUtils.write2Xml(document);
					return;
				}
			}
			throw new StudentNotExistException(name+"不存在!");
		}catch(StudentNotExistException e){
			throw e;
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}


xml文件格式

	<student examid="444" idcard="333">
		<name>***</name>
		<location>add</location>
		<grade>99</grade>
	</student>

猜你在找的XML相关文章