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());
System.out.print("继续注册(Y/N)?");
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]);
n
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
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder= factory. newDocumentBuilder();
Document document= builder.parse(new File("reg.xml"));
NodeList nodelist=document.getElementsByTagName("用户");
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 {
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");
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;
}
}谢谢了··那位高手可以帮下!!最好解释到每行····
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());
System.out.print("继续注册(Y/N)?");
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]);
n
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
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder= factory. newDocumentBuilder();
Document document= builder.parse(new File("reg.xml"));
NodeList nodelist=document.getElementsByTagName("用户");
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 {
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");
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 正則多行模式匹配小問題
- 如何spring jdbc 来连接并执行oracle 存储过程(带参)并返回结果
- 运行中出现这种错误,JSP调用EJB,用的是STRUTS加EJB架构,谢谢各位大哥
- java中有这个吗?System.enviorment.newline,好像System里没有啊。
- 到底哪里错?
- 奇怪问题??? 关于.jar 文件的执行????
- 求两个日期之间相差的天数 数据库是MYSQL
- 高分求救:java无法运行程序
- 请高手回答:怎么样取得当前时间+2小时!
- 反编译一个class,这句话怎么理解:static Class class$jnsoft$manager$Slog;
- 关于Applet的简单问题
- 求助JAVA聊天室问题
好像一個根據提示,讓你輸入你的個人信息,
然後再以文件的形式保存到xml文件裡面
另:顺便修正了1个BUGpackage 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;
}
}