package com.telephoneBook;
import java.io.FileOutputStream;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class TelephoneBook {
static Scanner input=new Scanner(System.in);
public static void main(String[] args) throws Exception {
new TelephoneBook().ChoiceOrder();
}
public void ChoiceOrder() throws Exception{
while(true){
System.out.println("*******************************************************************");
System.out.println("1.增加通讯录成员\n2.删除指定通讯录成员\n3.修改通讯录成员信息\n4.查找指定的通讯录成员\n5.输出通讯录成员列表\n0.退出程序");
System.out.println("*******************输出序号1/2/3/4/5****************************");
int md=input.nextInt();
if(md==1){
add();
}else if(md==2){
delete();
}else if(md==3){
modify();
}else if(md==4){
seek();
}else if(md==5){
peopleList();
}else if(md==0){
System.out.println("退出程序,感谢使用");
System.exit(1);
}else{
System.out.println("输入序号错误,请重新输入!");
ChoiceOrder();
}
}
}
/**
* 增加通讯录成员-----add
*/
public void add() throws Exception{
//解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//DOM树
Document doc=db.parse("src/TelephoneBook.xml");
//增加phone节点
Element phoneElement=doc.createElement("phone");
System.out.print("姓名:");
String name=input.next();
System.out.print("电话号码:");
String PhoneNum=input.next();
if(PhoneNum!=null){
phoneElement.setAttribute("name", name);
phoneElement.setAttribute("phonenumber", PhoneNum);
Element phonebookElement=(Element)doc.getElementsByTagName("phonebook").item(0);
phonebookElement.appendChild(phoneElement);
System.out.println("添加成功!");
}else{
System.out.println("号码为空,请输入号码,重新添加成员");
add();
}
/**
* 保存xml文件
*/
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
DOMSource domSource=new DOMSource(doc);
//设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
StreamResult streamResult=new StreamResult(new FileOutputStream("src/TelephoneBook.xml"));
//将DOM树转换为xml文件
transformer.transform(domSource, streamResult);
}
/**
* 删除通讯录指定成员-----delete
*/
public void delete() throws Exception{
//解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//DOM树
Document doc=db.parse("src/TelephoneBook.xml");
System.out.print("想要删除的成员姓名:");
String deleteName=input.next();
int flag=0;
//找到要删除的节点
NodeList nodeList=doc.getElementsByTagName("phone");
for(int i=0;i<nodeList.getLength();i++){
Element phoneElement=(Element)nodeList.item(i);
String name=phoneElement.getAttribute("name");
if(name.equals(deleteName)){
flag=1;
phoneElement.getParentNode().removeChild(phoneElement);
}
}
if(flag==1){
System.out.println("删除成功");
}else{
System.out.println("未找到指定要删除的姓名");
}
/**
* 保存xml文件
*/
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
DOMSource domSource=new DOMSource(doc);
//设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
StreamResult streamResult=new StreamResult(new FileOutputStream("src/TelephoneBook.xml"));
//将DOM树转换为xml文件
transformer.transform(domSource, streamResult);
}
/**
* 修改通讯录成员的信息-----modify
*/
public void modify() throws Exception{
//解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//DOM树
Document doc=db.parse("src/TelephoneBook.xml");
System.out.print("输入想要修改的成员姓名:");
String modifyName=input.next();
System.out.print("修改后的号码:");
String number=input.next();
//找到要修改的成员
NodeList phoneNodeList=doc.getElementsByTagName("phone");
for(int i=0;i<phoneNodeList.getLength();i++){
Element phoneElement=(Element)phoneNodeList.item(i);
String name=phoneElement.getAttribute("name");
if(name.equals(modifyName)){
phoneElement.setAttribute("phonenumber", number);
}
}
/**
* 保存xml文件
*/
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
DOMSource domSource=new DOMSource(doc);
//设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
StreamResult streamResult=new StreamResult(new FileOutputStream("src/TelephoneBook.xml"));
//将DOM树转换为xml文件
transformer.transform(domSource, streamResult);
}
/**
* 查找指定的成员信息-------seek
*/
public void seek() throws Exception{
//解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//DOM树
Document doc=db.parse("src/TelephoneBook.xml");
System.out.print("查找的成员姓名:");
String seekName=input.next();
int flag=0;
NodeList phoneNodeList=doc.getElementsByTagName("phone");
System.out.println("序号"+"\t\t"+"姓名"+"\t\t"+"电话号码");
for(int i=0;i<phoneNodeList.getLength();i++){
Element phoneNodeElement=(Element) phoneNodeList.item(i);
if(phoneNodeElement.getAttribute("name").equals(seekName)){
flag=1;
System.out.println(i+1+"\t\t"+seekName+"\t\t"+phoneNodeElement.getAttribute("phonenumber"));
}
}
if(flag==0){
System.out.println("查找成功");
}else{
System.out.println("查找失败");
}
}
/**
* 输出成员列表------peopleList
*/
public void peopleList() throws Exception{
//解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder db=dbf.newDocumentBuilder();
//DOM树
Document doc=db.parse("src/TelephoneBook.xml");
NodeList phoneNodeList=doc.getElementsByTagName("phone");
System.out.println("-------------列表如下----------------");
System.out.println("序号"+"\t\t"+"姓名"+"\t\t"+"电话号码");
for(int i=0;i<phoneNodeList.getLength();i++){
Element phoneElement=(Element)phoneNodeList.item(i);
System.out.println(i+1+"\t\t"+phoneElement.getAttribute("name")+"\t\t"+phoneElement.getAttribute("phonenumber"));
}
}
}
- 1
- 2
前往页