缺点是一开始做的做的时候没有考虑到面向对象 并没有写出一个Contact类把所有的联系人封装到对象中.
Demo1.java
package cn.itcast.dom4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Scanner;
/**
* Created by LiJing on 2017/9/4.
*/
public class Demo1 {
static Document document = null ;
static File file =new File("./contact.xml");
public static void main(String[] args) throws Exception{
while(true) {
init();
select();
write();
}
}
//初始化
public static void init()throws Exception{
document = new SAXReader().read(file);
System.out.println("*************************************");
System.out.println("* 1 . 添加联系人 *");
System.out.println("* 2 . 修改联系人 *");
System.out.println("* 3 . 删除联系人 *");
System.out.println("* Q . 查询所有联系人 *");
System.out.println("*************************************");
}
public static void select() throws Exception{
Scanner scanner = new Scanner(System.in);
String select = "";
if(scanner.hasNext()){
select = scanner.next();
}
if("1".equals(select)){
//添加联系人
add();
}else if("2".equals(select)){
//修改联系人
gai();
}else if("3".equals(select)){
//删除联系人
String id = null;
System.out.println("请输入要删除的ID");
Scanner scanner1 = new Scanner(System.in);
if(scanner1.hasNext()){
id = scanner1.next();
}
del(id);
}else if("q".equalsIgnoreCase(select)) {
//查询所有联系人
getAll();
}else{
System.out.println("您的输入有误");
}
}
//增加联系人
public static void add() throws Exception{
Scanner scanner = new Scanner(System.in);
String text = null;
Element rootElem = document.getRootElement();
Element contactElem = rootElem.addElement("contact");
Element idElem = contactElem.addElement("id");
System.out.println("请输入编号");
if(scanner.hasNext()){
text = scanner.next();
}
contactElem.attribute("id").setText(text);
Element nameElem = contactElem.addElement("name");
System.out.println("请输入姓名");
if(scanner.hasNext()){
text = scanner.next();
}
nameElem.addText(text);
Element sexElem = contactElem.addElement("sex");
System.out.println("请输入性别");
if(scanner.hasNext()){
text = scanner.next();
}
sexElem.addText(text);
Element ageElem = contactElem.addElement("age");
System.out.println("请输入年龄");
if(scanner.hasNext()){
text = scanner.next();
}
ageElem.addText(text);
Element phoneElem = contactElem.addElement("phone");
System.out.println("请输入手机号");
if(scanner.hasNext()){
text = scanner.next();
}
phoneElem.addText(text);
Element emailElem = contactElem.addElement("email");
System.out.println("请输入邮箱");
if(scanner.hasNext()){
text = scanner.next();
}
emailElem.addText(text);
Element qqElem = contactElem.addElement("qq");
System.out.println("请输入QQ");
if(scanner.hasNext()){
text = scanner.next();
}
qqElem.addText(text);
System.out.println("添加成功");
}
public static void del(String id){
Element rootElem = document.getRootElement();
List list = rootElem.elements();
for(Element element:list){
if(element.element("id").getText().equals(id)){
element.detach();
}
}
System.out.println("删除成功");
}
public static void gai() throws Exception {
String text = null;
String id = null ;
System.out.println("请输入要修改的ID");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()){
id = scanner.next();
}
Element contactElem = (Element) document.selectSingleNode("//contact[@id=" + id + "]");
Element nameElem = contactElem.element("name");
System.out.println("请输入姓名");
if(scanner.hasNext()){
text = scanner.next();
}
nameElem.setText(text);
Element sexElem = contactElem.element("sex");
System.out.println("请输入性别");
if(scanner.hasNext()){
text = scanner.next();
}
sexElem.setText(text);
Element ageElem = contactElem.element("age");
System.out.println("请输入年龄");
if(scanner.hasNext()){
text = scanner.next();
}
ageElem.setText(text);
Element phoneElem = contactElem.element("phone");
System.out.println("请输入手机号");
if(scanner.hasNext()){
text = scanner.next();
}
phoneElem.addText(text);
Element emailElem = contactElem.element("email");
System.out.println("请输入邮箱");
if(scanner.hasNext()){
text = scanner.next();
}
emailElem.setText(text);
Element qqElem = contactElem.element("qq");
System.out.println("请输入QQ");
if(scanner.hasNext()){
text = scanner.next();
}
qqElem.setText(text);
System.out.println("修改成功");
}
public static void write() throws Exception{
//设置输出路径
FileOutputStream fileOutputStream = new FileOutputStream(file);
//美观输出
OutputFormat format = OutputFormat.createPrettyPrint();
//编码
format.setEncoding("utf-8");
//创建
XMLWriter writer = new XMLWriter(fileOutputStream,format);
//写出
writer.write(document);
}
//获取所有联系人
public static void getAll(){
String xpath = "//contact";
List list =document.selectNodes(xpath);
System.out.println("编号 姓名 性别 年龄 电话 邮箱 QQ");
for(Element element:list){
//获取到的是tr节点 然后获取子节点的文本 需要强转
System.out.print(element.attributeValue("id"));
System.out.print(" "+((Element) element.elements().get(0)).getText());
System.out.print(" " + ((Element) element.elements().get(1)).getText());
System.out.print(" "+((Element)element.elements().get(2)).getText());
System.out.print(" " + ((Element) element.elements().get(3)).getText());
System.out.print(" " + ((Element) element.elements().get(4)).getText());
System.out.println(" "+((Element)element.elements().get(5)).getText());;
}
}
}
contact.xml
<contactList>
<contact id="001">
<name>lijingname>
<sex>nnasex>
<age>14age>
<phone>1232224451phone>
<email>[email protected]email>
<qq>123456qq>
contact>
<contact id="002">
<name>张三name>
<sex>男sex>
<age>18age>
<phone>123111123123123123phone>
<email>[email protected]email>
<qq>123444444qq>
contact>
contactList>