A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

这是xml
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <exam>
  3.    <student examid="222" idcard="111">
  4.       <name>张三</name>
  5.       <location>襄阳</location>
  6.       <score>78</score>
  7.    </student>
  8.    
  9.    <student examid="555" idcard="333">
  10.       <name>李四</name>
  11.       <location>襄阳</location>
  12.       <score>65</score>
  13.    </student>
  14. </exam>
复制代码


StudentDAO中的add方法
  1.         //向xml中添加数据
  2.    public static void add(Student stu)
  3.    {
  4.           
  5.         try {
  6.                 Document docu = XmlUtils.getDocument();
  7.                 //得到Document对象
  8.                    Element elstudent = docu.createElement("student");//创建节点并添加属性
  9.                    elstudent.setAttribute("idcard", stu.getIdcard());
  10.                    elstudent.setAttribute("examid", stu.getExamid());
  11.                    //创建子节点,并设计值
  12.                    Element name = docu.createElement("name");
  13.                    name.setTextContent(stu.getName());
  14.                    Element location = docu.createElement("location");
  15.                    location.setTextContent(stu.getLocation());
  16.                    Element score = docu.createElement("score");
  17.                    score.setTextContent(stu.getScore());
  18.                    //将子节点添加到父节点中
  19.                    elstudent.appendChild(name);
  20.                    elstudent.appendChild(location);
  21.                    elstudent.appendChild(score);
  22.                   
  23.                    //在根节点上挂就是appendchild抛的异常DOMException
  24.                    XmlUtils.getElement().appendChild(elstudent);
  25.                   
  26.                    XmlUtils.Write2Xml(docu);//将内存中的数据写入到xml中
  27.         } catch (Exception e) {
  28.                 throw new RuntimeException(e);//将编译事异常转换为运行时异常
  29.         }
  30.           
  31.    }
复制代码


Test中的添加方法:
  1.         Scanner sc=new Scanner(System.in);
  2.             System.out.println("请输入学生姓名:");
  3.             String name=sc.nextLine();
  4.             System.out.println("请输入学生身份证号码:");
  5.             String idcard=sc.nextLine();
  6.             System.out.println("请输入学生准考证号码:");
  7.             String examid=sc.nextLine();
  8.             System.out.println("请输入学生所在地:");
  9.             String location=sc.nextLine();
  10.             System.out.println("请输入学生成绩:");
  11.             String score=sc.nextLine();
  12.             Student stu=new Student(idcard, examid, name, location, score);
  13.            
  14.                         try {
  15.                                 StudentDAO.add(stu);
  16.                         } catch (Exception e) {
  17.                                 e.printStackTrace();
  18.                         }
复制代码

真不知道哪里错了

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

2 个回复

正序浏览
明白了,谢谢,说的是有理的,解决了
回复 使用道具 举报
不知道你XmlUtils类是怎么写的,XmlUtils.getElement().appendChild(elstudent);这里会出现异常,有可能是你XmlUtils类中getDocument方法返回的Document对象和getElement方法使用的Document对象不是同一个。

评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马