一段操作xml的代码

从键盘读入数据,并保存到xml里面

改造自 wind70713 http://topic.csdn.net/u/20071230/16/a3517180-5229-49a9-8006-c52747a7b373.html
package test;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

public class JAXPTwo {
  public static void main(String args[]) {
    try {
      while (true) {
        RegInsert(InsertInfo()); // 输入数据并保存到xml文件
        System.out.print("继续注册(Y/N)?"); // 继续下一个?
        if ("N".equalsIgnoreCase(readInput())) // if (readInput() == "N")
          break;
      }
    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static String[] InsertInfo() {
    String[] array = new String[10];
    // String temp; // 这个变量没有使用???
    try {
      System.out.print("请输入姓名:"); // 开始输入各种信息
      array[0] = readInput(); // 从键盘读取信息

      System.out.print("请输入生日:");
      array[1] = readInput();

      // while(true)
      // {
      System.out.print("请输入邮箱:");
      array[2] = readInput();
      // if(CheckMail(array[2]))
      // System.out.print("该邮箱已经注册");
      // else
      // break;
      // }
      System.out.print("请输入密码:");
      array[3] = readInput();
      // System.out.print(array[3]); // 这一句去掉吧

      System.out.print("请输入毕业学校:");
      array[4] = readInput();

      System.out.print("请输入毕业年份:");
      array[5] = readInput();

      System.out.print("请输入通讯地址:");
      array[6] = readInput();

      System.out.print("请输入书籍:");
      array[7] = readInput();

      System.out.print("请输入婚姻状况:");
      array[8] = readInput();
    } catch (Exception e) {
      System.out.println(e);
    }

    return array;
  }

  public static boolean CheckMail(String mail) {
    boolean temp = false;
    try {

      // 打开reg.xml
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      Document document = builder.parse(new File("reg.xml"));

      // 拿到所有的用户节点
      NodeList nodelist = document.getElementsByTagName("用户");

      // 循环判断是否存在重复的email

      for (int k = 0; k < nodelist.getLength(); k++) {
        Node node = nodelist.item(k);
        NodeList child = node.getChildNodes();
        String tmail = child.item(5).getFirstChild().getNodeValue();
        if (tmail == mail) {
          temp = true;
          break;
        }
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    return temp;
  }

  public static void RegInsert(String array[]) {
    try {

      // 解析reg.xml文件
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();

      Document document = builder.parse(new File("reg.xml"));

      // 得到根节点
      Element root = document.getDocumentElement();

      // 创建用户节点
      Element userNode = document.createElement("用户");

      // 创建下面的子节点
      Element nameNode = document.createElement("姓名");
      Element birthdayNode = document.createElement("生日");
      Element mailNode = document.createElement("邮箱");
      Element passwordNode = document.createElement("密码");
      Element schoolNode = document.createElement("毕业学校");
      Element datatimeNode = document.createElement("毕业年份");
      Element addressNode = document.createElement("通讯地址");
      Element bookNode = document.createElement("书籍");
      Element marryNode = document.createElement("婚姻状况");

      // 创建节点的值
      Text nameValue = document.createTextNode(array[0]);
      Text birthdayValue = document.createTextNode(array[1]);
      Text mailValue = document.createTextNode(array[2]);
      Text passwordValue = document.createTextNode(array[3]);
      Text schoolValue = document.createTextNode(array[4]);
      Text datatimeValue = document.createTextNode(array[5]);
      Text addressValue = document.createTextNode(array[6]);
      Text bookValue = document.createTextNode(array[7]);
      Text marryValue = document.createTextNode(array[8]);

      // 节点与节点值对应
      nameNode.appendChild(nameValue);
      birthdayNode.appendChild(birthdayValue);
      mailNode.appendChild(mailValue);
      passwordNode.appendChild(passwordValue);
      schoolNode.appendChild(schoolValue);
      datatimeNode.appendChild(datatimeValue);
      addressNode.appendChild(addressValue);
      bookNode.appendChild(bookValue);
      marryNode.appendChild(marryValue);

      // 子节点放到用户节点下
      userNode.appendChild(nameNode);
      userNode.appendChild(birthdayNode);
      userNode.appendChild(mailNode);
      userNode.appendChild(passwordNode);
      userNode.appendChild(schoolNode);
      userNode.appendChild(datatimeNode);
      userNode.appendChild(addressNode);
      userNode.appendChild(bookNode);
      userNode.appendChild(marryNode);

      // 用户放到根节点
      root.appendChild(userNode);

      TransformerFactory transFactory = TransformerFactory.newInstance();
      Transformer transformer = transFactory.newTransformer();

      // 传输的编码为中文
      transformer.setOutputProperty("encoding", "gb2312");

      // 保存到文件reg.xml
      DOMSource domSource = new DOMSource(document);
      File file = new File("reg.xml");
      FileOutputStream out = new FileOutputStream(file);
      StreamResult xmlResult = new StreamResult(out);
      transformer.transform(domSource, xmlResult);

    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static String readInput() throws IOException {
    BufferedReader br;
    String str = "";
    // 从键盘读取一行输入,回车结束
    br = new BufferedReader(new InputStreamReader(System.in));
    str = br.readLine();
    return str;
  }
}
JAVA世纪网
愿意为喜欢JAVA的朋友提供一点帮助