本帖最后由 354620815 于 2015-1-13 00:07 编辑
- package com.itheima;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- public class ParseXml_DOM4J {
- /**
- * 解析exam.xml文件实现以下功能:
- * 添加学生信息
- * 删除学生信息
- * 查询学生信息
- * DOM4j传统的方式:
- * 1、创建解析器:new SAXReader();
- * 2、用解析器去读取xml到内存:read();
- * 3、获取根节点:getRootElement();
- * elements():查找元素中的所有元素;
- * attributes():查找元素中的所有属性
- * remove():删除元素,父删子;
- * addAttribute():给元素添加属性;
- *
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- hint();
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- // 创建一个解析器
- SAXReader saxR = new SAXReader();
- // 加载XML文件
- Document document = saxR.read("src/exam.xml");
- //读取键盘,判断读取到的字符串,并执行相关操作
- while((line = bufr.readLine())!=null){
- if("a".equals(line)){
- addStu(bufr,document);
- }else if("b".equals(line)){
- delStu(bufr,document);
- }else if("c".equals(line)){
- selectStu(bufr,document);
- }else if("over".equals(line)){
- break;
- }else {
- System.out.println("操作有误...请重新输入...");
- }
- }
- }
- /**
- * 提醒用户
- */
- public static void hint(){
- System.out.println("添加用户(a) 删除用户(b) 查询用户(c)");
- System.out.println("请输入操作类型");
- }
- /**
- * 查找学生
- * @param bufr
- * @param document
- * @throws Exception
- */
- private static void selectStu(BufferedReader bufr, Document document) throws Exception {
- boolean flag = true;
- System.out.println("请输入准考证号:");
- String examid = bufr.readLine();
- Element root = document.getRootElement();
- List<Element> stuEles = root.elements("student");
- for(Element stuEle : stuEles){
- if(examid.equals(stuEle.attribute("examid").getText())){
- System.out.println("姓名:" + stuEle.element("name").getText());
- System.out.println("身份证:" + stuEle.attribute("idcard").getText());
- System.out.println("准考证:" + stuEle.attribute("examid").getText());
- System.out.println("地区:" + stuEle.element("location").getText());
- System.out.println("成绩:" + stuEle.element("grade").getText());
- flag = false;
- }
- }
- if(flag){
- System.out.println("找不到...");
- selectStu(bufr,document);
- }else{
- hint();
- }
- }
- /**
- * 删除学生
- * @param bufr
- * @param document
- * @throws Exception
- */
- private static void delStu(BufferedReader bufr, Document document) throws Exception {
- boolean flag = false;
- System.out.println("请输入要删除的学生姓名:");
- String name = bufr.readLine();
- //获取根节点
- Element root = document.getRootElement();
- //获取所有学生节点
- List<Element> stuEles = root.elements("student");
- for(Element stuEle : stuEles){
- if(name.equals(stuEle.element("name").getText())){
- root.remove(stuEle);
- flag = true;
- }
- }
- if(flag){
- write2xml(document);
- System.out.println("......删除成功......");
- hint();
- }else {
- System.out.println("......删除失败......");
- delStu(bufr,document);
- }
- }
- /**
- * 添加学生
- * @param bufr
- * @param document
- * @throws Exception
- */
- public static void addStu(BufferedReader bufr, Document document) throws Exception{
- System.out.println("请输入姓名:");
- String name = bufr.readLine();
- System.out.println("请输入准考证号:");
- String examid = bufr.readLine();
- System.out.println("请输入身份证:");
- String idcard = bufr.readLine();
- System.out.println("请输入所在地:");
- String location = bufr.readLine();
- System.out.println("请输入成绩:");
- String grade = bufr.readLine();
- //获取根节点
- Element root = document.getRootElement();
- //添加学生节点和属性
- Element studentEle = root.addElement("student").addAttribute("idcard", idcard)
- .addAttribute("examid", examid);
- //添加名字节点
- studentEle.addElement("name").setText(name);
- //添加地址节点
- studentEle.addElement("location").setText(location);
- //添加成绩节点
- studentEle.addElement("grade").setText(grade);
- //写到xml中
- write2xml(document);
- System.out.println("....添加成功......");
- hint();
- }
- /**
- * 将内存的文档写到xml中
- * @param document
- * @throws Exception
- * @throws FileNotFoundException
- */
- public static void write2xml(Document document) throws Exception, FileNotFoundException{
- XMLWriter write = new XMLWriter(new FileOutputStream("src/exam.xml"),
- OutputFormat.createPrettyPrint());
- write.write(document);
- write.close();
- }
-
- }
复制代码 xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
|