XML实现简单学生管理系统

使用xml当作数据库,存储学生信息

// 增加
	public static void addStu(StudentDemo student) throws DocumentException, IOException {
		/*
		 * 1、创建解析器
		 * 2、获得document
		 * 3、获得根节点
		 * 4、在根节点上添加stu标签
		 * 5、在stu标签上依次添加id、name、age
		 * 6、在id、name、age上面依次添加值
		 * 7、回写xml
		 */
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/student.xml");
		Element root = document.getRootElement();
		
		Element stu = root.addElement("stu");
		Element id = stu.addElement("id");
		Element name = stu.addElement("name");
		Element age = stu.addElement("age");
		
		id.setText(student.getId());
		name.setText(student.getName());
		age.setText(student.getAge());
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}
//删除
	//根据student id删除
	
	public static void 	delStu(String id) throws DocumentException, IOException {
		/*
		 * 1、创建解析器
		 * 2、获得document
		 * 3、获得id  通过xpath
		 * 4、遍历list集合
		 * 5、判断list内id和传递的id是否相同
		 * 6、如果相同,把id所造stu删除
		 * 7、回写xml
		 */
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/student.xml");
		
		List<Node> list = document.selectNodes("//id");
		
		for (Node node : list) {
			String idv = node.getText();
			if(idv.equals(id)) {
				Element stu = node.getParent();
				Element student = stu.getParent();
				student.remove(stu);
			}
		}
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
		xmlWriter.write(document);
		xmlWriter.close();
	}
//查询 更具id查询
	
	public static StudentDemo selectStu(String id) throws DocumentException {
		/*
		 * 1、创建解析器
		 * 2、获得document
		 * 3、获得所有id通过xpath
		 * 4、遍历list集合
		 * 5、判断list内id和传递的id是否相同
		 * 6、相同则找到父节点stu
		 * 7、得到stu的id,name,age值
		 */
		StudentDemo student = new StudentDemo();
		
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/student.xml");
		
		List<Node> list = document.selectNodes("//id");
		
		for (Node node : list) {
			String idv = node.getText();
			
			if(idv.equals(id)) {
				Element stu = node.getParent();
				String namev = stu.element("name").getText();
				String agev = stu.element("age").getText();
				student.setId(id);
				student.setName(namev);
				student.setAge(agev);
			}
			
		}
		
		return student;
	}

XML——dom4j

练习xml文本

<?xml version="1.0" encoding="UTF-8"?>
<person>
	<p1>
		<name>张三</name>
		<age>20</age>
	</p1>
	
	<p1>
		<name>李四</name>
		<age>30</age>
	</p1>
</person>

dom4j 查询name内的所有信息

  • 创建解析器
  • 得到document
  • 得到根节点 getRootElement()
  • 得到所有的p1的标签{element(qname)//表示标签下第一个子标签,elements(qname)//获取标签下面的所有子标签(一层}
  • 得到name
  • 得到name里面的值
public static void selectName() throws DocumentException {
		/*
		 * 1、创建解析器
		 * 2、得到document
		 * 3、得到根节点
		 * 4、得到p1
		 * 5、得到p1下面的name
		 * 6、得到name里面的值
		 */
		
		SAXReader saxReader = new SAXReader();
		
		Document document = saxReader.read("src/Student.xml");
		
		Element root = document.getRootElement();
		
		List<Element> p1 = root.elements("p1");
		
		for (Element element : p1) {
			Element name1 = element.element("name");
			String s = name1.getText();
			System.out.println(s);
		}
	}

dom4j实现末尾添加操作

/*添加性别条目*/

  • 创建解析器
  • 得到document
  • 得到根节点
  • 获取到第一个p1
  • 在p1下边添加元素
  • 在添加完成之后的元素下面添加文本
  • 回写xml文件
public static void addSex() throws DocumentException, IOException {
		SAXReader saxReader = new SAXReader();
		
		Document document = saxReader.read("src/Student.xml");
		
		Element root = document.getRootElement();
		
		Element p1 = root.element("p1");
		//在p1下边儿直接创建一个元素
		
		Element sex1 = p1.addElement("sex");
		sex1.setText("nv");
		
		//回写xml
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/Student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
		
	}

dom4j在特定位置添加元素

/*在第一个p1下面的age标签之前添加<school>uestc</school>*/

  • 创建解析器
  • 得到document
  • 得到根节点
  • 获取到第一个p1
  • 获得p1下面的所有元素
    • **elements()返回List;
    • List特定位置添加元素
    • add(int index, E element)
  • 回写xml
public static void addAgeBefore() throws DocumentException, IOException {
		SAXReader saxReader = new SAXReader();
		
		Document document = saxReader.read("src/Student.xml");
		
		Element root = document.getRootElement();
		
		Element p1 = root.element("p1");
		
		List<Element> list = p1.elements();
		
		Element school = DocumentHelper.createElement("school");
		school.setText("UESTC");
		list.add(1,school);
		//这些操作都在内存中操作的,所以接下来回写xml
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/Student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}

dom4j实现修改操作

/*修改p1下age的值为60*/

  • 创建解析器
  • 得到document
  • 得到根节点
  • 获取到第一个p1下的age
  • 修改值为60
  • 回写xml
public static void modify() throws DocumentException, IOException {
		SAXReader saxReader = new SAXReader();
		
		Document document = saxReader.read("src/Student.xml");
		
		Element root = document.getRootElement();
		
		Element p1 = root.element("p1");
		
		Element age = p1.element("age");
		
		age.setText("60");
		
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/Student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}

dom4j删除节点操作——通过父节点删除

/*删除学校*/

  • 创建解析器
  • 得到document
  • 得到根节点
  • 得到p1标签
  • 得到p1下面的school元素
  • 删除
  • 回写xml
public static void delet() throws DocumentException, IOException {
		SAXReader saxReader = new SAXReader();
		
		Document document = saxReader.read("src/Student.xml");
		
		Element root = document.getRootElement();
		
		Element p1 = root.element("p1");
		
		Element school = p1.element("school");
		//通过父节点删除
		
		//school.getParent();
		p1.remove(school);

		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/Student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}