00 06Java case of Web Development

1 student management system implementation - add operation

Use xml as a database that stores student information

Create a xml file, store some student information.

Add student information.
(1) Creating a parser
(2) to give Document
(. 3) acquires the root node
(4) created on the root node stu element
(5) was added in stu id name age element
(6) are sequentially added value id name age
(. 7) xml write back

public static boolean addStudent(Student stu) {
		try {
			Document document = getDocument();
			
			Element root = document.getRootElement();
			
			Element stuNew = DocumentHelper.createElement("stu");
			Element idNew = DocumentHelper.createElement("id");
			Element nameNew = DocumentHelper.createElement("name");
			Element ageNew = DocumentHelper.createElement("age");
			
			idNew.setText("" + stu.getId());
			nameNew.setText(stu.getName());
			ageNew.setText("" + stu.getAge());
			
			stuNew.add(idNew);
			stuNew.add(nameNew);
			stuNew.add(ageNew);
			root.add(stuNew);
			
			xmlWriters(document);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

2 student management system implementation - delete

private static Element findStuElementById(Document document, long id) {
		
		List<Node> stuIds = document.selectNodes("//stu/id");

		for(Node node : stuIds) {
			if(Long.parseLong(node.getText()) == id) {
				return node.getParent();
			}
		}
		return null;
	}

Id remove students according to the students.
(1) Creating a parser
(2) to give Document
(. 3) acquired all id
(. 4) traversing the list set
(5) Analyzing id and the id of the transfer set which are the same
(6) If the same, where the STU id deleted

public static void removeStudent(long id) throws Exception{
		Document document = getDocument();
		Element element = StudentControl.findStuElementById(document, id);
		Element root = element.getParent();
		root.remove(element);
		xmlWriters(document);
	}

3 student management system implementation - query

According to student information query id.
(1) Creating a parser
(2) to give Document
(. 3) acquired all id
(. 4) returns the list set, collection traversal list
(5) to give each node id
values (. 6) id node
(7) is determined id id value transfer and value are the same
(8) If the same, the first acquired parent node id stu
value (9) is obtained by the name age stu

public static Student searchStudent(long id) throws Exception{
		Student stu = null;
		Document document = getDocument();
		Element stuNode = StudentControl.findStuElementById(document, id);
		stu = new Student(Long.parseLong(stuNode.element("id").getText()), 
				stuNode.element("name").getText(),
				Integer.parseInt(stuNode.element("age").getText()));	
		return stu;
	}
Published 77 original articles · won praise 11 · views 2596

Guess you like

Origin blog.csdn.net/weixin_43762330/article/details/104533603