1. 程式人生 > >用dom4j實現xml操作

用dom4j實現xml操作

要匯入的包: 在這裡插入圖片描述 dom4jUtil.java

package utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class dom4jUtil
{ public static Document getDocument() throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/exam.xml")); return document; } public static void updateXML(Document document) throws IOException { OutputFormat format = OutputFormat.
createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/exam.xml"),format); writer.write(document); writer.close(); } }

dom4jDao.java

package dao;

import java.util.Iterator;
import java.util.List;

import org.dom4j.
Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import domain.Student; import utils.dom4jUtil; public class dom4jDao { public void add(Student s) { Document document; try { document = dom4jUtil.getDocument(); Element studentNode = DocumentHelper.createElement("student"); studentNode.addAttribute("examid", s.getExamid()); studentNode.addAttribute("idcard", s.getIdcard()); //向指定節點上掛節點的便捷方式 studentNode.addElement("name").setText(s.getName()); studentNode.addElement("location").setText(s.getLocation()); studentNode.addElement("gradeNode").setText(s.getGrade()+""); document.getRootElement().add(studentNode); dom4jUtil.updateXML(document); } catch (Exception e) { throw new RuntimeException(); } } public void delete(String name){ try { Document document = dom4jUtil.getDocument(); List list = document.selectNodes("//name"); Iterator it = list.iterator(); while(it.hasNext()) { Element name_node = (Element) it.next(); if(name_node.getText().equals(name)) { name_node.getParent().getParent().remove(name_node.getParent()); dom4jUtil.updateXML(document); return; } } throw new RuntimeException("找不到要刪除的學生!"); } catch (Exception e1) { throw new RuntimeException(e1); } } public Student find(String examid) { try { Document document = dom4jUtil.getDocument(); Element node = (Element) document.selectSingleNode("//student[@examid='"+examid+"']"); if(node!=null) { Student s = new Student(); s.setExamid(node.attributeValue("examid")); s.setIdcard(node.attributeValue("idcard")); s.setName(node.element("name").getText()); s.setLocation(node.element("location").getText()); s.setGrade(Double.parseDouble(node.element("grade").getText())); return s; }else { return null; } } catch (DocumentException e1) { throw new RuntimeException(e1); } } }

Main.java

package view;

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

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.dom4j.DocumentException;
import org.xml.sax.SAXException;

import dao.dom4jDao;
import domain.Student;

public class Main {
	public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException, DocumentException {
		System.out.println("新增使用者:(a) 刪除使用者:(b) 查詢成績:(c)");
		System.out.println("請輸入操作型別:");
		
		Student student = new Student();
		//StudentDao stuDao = new StudentDao();
		dom4jDao domDao = new dom4jDao();
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String type = bf.readLine();
		if(type.equalsIgnoreCase("a")) {
			System.out.println("請輸入學生姓名:");
			student.setName(bf.readLine());
			System.out.println("請輸入學生准考證號:");
			student.setExamid(bf.readLine());
			System.out.println("請輸入學生身份證號:");
			student.setIdcard(bf.readLine());
			System.out.println("請輸入學生所在地:");
			student.setLocation(bf.readLine());
			System.out.println("請輸入學生成績:");
			student.setGrade(Double.parseDouble(bf.readLine()));
			System.out.println("------新增資料成功------");	
			
			//stuDao.add(student);
			domDao.add(student);
		}else if(type.equalsIgnoreCase("b")) {
			System.out.println("請輸入刪除的學生姓名:");
			//stuDao.delete(bf.readLine());
			domDao.delete(bf.readLine());
		}else if(type.equalsIgnoreCase("c")) {
			System.out.println("請輸入查詢的學生的准考證號:");
			
			//stuDao.find(bf.readLine());
			domDao.find(bf.readLine());
			System.out.println("您查詢的學生資訊為:");
			System.out.println("姓名:"+student.getName()+",身份證號:"+student.getIdcard()+",准考證號:"+student.getExamid()+",地區:"+student.getLocation()+",成績:"+student.getGrade());
		}else {
			System.out.println("沒有");
		}
	}
}