Xml的CRUD操作

exam.xml:

<?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:

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) {


}
}
}


工具类 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)));
}
}


你可能感兴趣的:(Xml的CRUD操作)