Xml的CRUD操做

exam.xml: java

<?xml version="1.0" encoding="UTF-8"?>
<exam>
<student idcard="111" examid="222">
<name>張三丰</name>
<location>杭州</location>
<grade>98</grade>
</student>
<student idcard="333" examid="444">
<name>張無忌</name>
<location>臨洮</location>
<grade>98</grade>
</student>
</exam>
編程

DAO: app

public class StudentDao {


public void add(Student student) {
Document document = null;
try {
document = XmlUtils.getDocument();
// 建立封裝Student信息的標籤
Element studentTag = document.createElement("student");
// 設置相應屬性
studentTag.setAttribute("idcard", student.getIdcard());
studentTag.setAttribute("examid", student.getExamid());


// 建立用於封閉學生姓名 所在地 成績的 標籤
Element name = document.createElement("name");
Element location = document.createElement("location");
Element grade = document.createElement("grade");


// 爲標籤設置文本內容
name.setTextContent(student.getName());
location.setTextContent(student.getLocation());
grade.setTextContent(student.getGrade() + "");


// 添加子點節點
studentTag.appendChild(name);
studentTag.appendChild(location);
studentTag.appendChild(grade);


// 將student添加到xml文檔中
document.getElementsByTagName("exam").item(0)
.appendChild(studentTag);
// studentTag.getParentNode().appendChild(studentTag);


// 將更新後的dom寫入內存中
XmlUtils.write2Xml(document);
} catch (Exception e) {
// 編程中咱們應當儘可能抓取運行時異常,避免往上層拋出編譯異常
throw new RuntimeException(e); // 將異常轉型爲運行時異常
}


}


/**
* 根據examid查詢相應學生記錄

* @param examid
* @return
*/
public Student find(String examid) {


try {
Document document = XmlUtils.getDocument();
// 獲取全部student集合
NodeList list = document.getElementsByTagName("student");
// 對list進行遍歷,查找匹配記錄
for (int i = 0; i < list.getLength(); i++) {
// new Student ,封裝信息
Element stu = (Element) list.item(i);


if (stu.getAttribute("examid").equals(examid)) {
// 如有匹配記錄,new Student,取出相應屬性值 ,返回記錄
Student student = new Student();
student.setExamid(examid);
student.setIdcard(stu.getAttribute("idcard"));
student.setName(stu.getElementsByTagName("name").item(0)
.getTextContent());
student.setLocation(stu.getElementsByTagName("location")
.item(0).getTextContent());
student.setGrade(Double.parseDouble(stu
.getElementsByTagName("grade").item(0)
.getTextContent()));


return student;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
// return null;
}


/**
* 刪除學生

* @param name
*/


public void delStudent(String name) throws StudentNotExistsException {
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");


for (int i = 0; i < list.getLength(); i++) {
Element tag = (Element) list.item(i);
if (tag.getTextContent().equals(name)) {
tag.getParentNode().getParentNode()
.removeChild(tag.getParentNode());


XmlUtils.write2Xml(document);
return;
}
}
throw new StudentNotExistsException(name + "不存在");
} catch (StudentNotExistsException e) {
throw e;
} catch (Exception e) {


}
}
}
dom


工具類 XmlUtils.java: 工具

/**
 * 工具類,全部方法爲靜態方法
 * 
 * @author zb
 * 
 */
public class XmlUtils {
private static String filename = "/exam.xml";


/**
* 獲取一個document對象

* @return
*/
public static Document getDocument() throws Exception {
// 實例DocumentBuilderFactory工廠
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 建立解析器
DocumentBuilder builder = factory.newDocumentBuilder();
// 獲取DOM對象
Document document = builder.parse(filename);


return document;
}


/**
* 將改動後的document對象寫入到xml文檔中

* @param document
* @throws TransformerException
* @throws FileNotFoundException
*/
public static void write2Xml(Document document)
throws FileNotFoundException, TransformerException {
// 實例化轉換器工廠
TransformerFactory factory = TransformerFactory.newInstance();
// 獲取轉換器
Transformer trans = factory.newTransformer();
// 轉換dom
trans.transform(new DOMSource(document), new StreamResult(
new FileOutputStream(filename)));
}
}

ui

相關文章
相關標籤/搜索