⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlwrite.java

📁 在线考试系统可以进行在线考试减少资源浪费(级jsp+xml)
💻 JAVA
字号:
package xmlTest;

import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.List;


public class XmlWrite {

  public XmlWrite() {
  }

  boolean flag=false;

  //创建xml文档的方法,参数userInfomation是UserInfo类型的对象,它记录了用户的信息。
  //在这里要通过这个对象将获取用户信息并将信息写入到xml文档中
  public boolean creatXml(UserInfo userInfomation)throws Exception
  {
     SAXBuilder sb = new SAXBuilder();
       UserInfo userInfo = userInfomation;

       //这一段代码是用来创建xml文档中的节点元素
       Element user = new Element("用户");
       Element logName = new Element("登陆名");
       Element name = new Element("姓名");
       Element age = new Element("年龄");
       Element sex = new Element("性别");
       Element job = new Element("职业");
       Element email = new Element("Email");
       Element comment = new Element("简介");
       Element schoolAge = new Element("教龄");
       Element position = new Element("职称");
       Element rank = new Element("军衔");
       Element armyType= new Element("军种");

      Document doc = null;

       //构造一个Document, 其根元素是“user”
       doc=new Document(user);

       user.addChild(logName);//在根节点下面添加子节点"登陆名"
       user.addChild(name);//在根节点下面添加子节点"姓名"
       user.addChild(age);//在根节点下面添加子节点"年龄"
       user.addChild(sex);//在根节点下面添加子节点"性别"
       user.addChild(job);//在根节点下面添加子节点"工作"
       user.addChild(email);//在根节点下面添加子节点"email"
       user.addChild(comment);//在根节点下面添加子节点"简介"

       //下面的这一段代码是给每个节点添加数据内容
       logName.setText(userInfo.getLogName());
       name.setText(userInfo.getName());
       age.setText(String.valueOf(userInfo.getAge()));
       sex.setText(userInfo.getSex());
       job.setText(userInfo.getJob());
       email.setText(userInfo.getEmail());
       comment.setText(userInfo.getComments());

       //职业不同时,会有不同的属性
       if(userInfo.getJob().equals("教师"))
       {
         //将"教龄","职称"等附加信息作为"职业"节点的属性
         job.addAttribute("教龄",userInfo.getSchoolAge());
         job.addAttribute("职称",userInfo.getPosition());
       }

       if(userInfo.getJob().equals("军人"))
       {
         job.addAttribute("军种",userInfo.getArmyType());
         job.addAttribute("军衔",userInfo.getRank());
       }

       String indent = "";
       boolean newLines = true;

       //创建一个XMLOutputter对象
       XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");

       //将doc对象输出到我们事先准备好的一个空的xml文档中
       outp.output(doc, new FileOutputStream("c:/user.xml"));

       //如果创建xml文档成功则返回true值
       flag=true;
       return  flag;

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -