import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Book {
private int no;
private String name;
private double value;
public Book() {
}
public Book(int no, String name, double value) {
this.no = no;
this.name = name;
this.value = value;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
class BookList {
private List<Book> bookList;
public BookList() {
bookList = readXML();
}
public long getCount() {
return bookList.size();
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
public void add(Book book) {
bookList.add(book);
}
public boolean delete(String name) {
Book book = query(name);
return bookList.remove(book);
}
public void update(Book bookBefore, Book bookAfter) {
bookList.remove(bookBefore);
add(bookAfter);
}
public Book query(String name) {
Book temp = null;
for (Book book : bookList) {
if (book.getName().equals(name)) {
temp = book;
}
}
return temp;
}
public synchronized void writeXmlDocument(Book book) {
try {
File file = new File("D:\\book.xml");
Document document = null;
Element root = null;
if (!file.exists()) {
// 新建student.xml文件并新增内容
document = DocumentHelper.createDocument();
root = document.addElement("Books");//添加根节点
} else {
SAXReader saxReader = new SAXReader();
document = saxReader.read(file);
root = document.getRootElement();//获得根节点
}
Element secondRoot = root.addElement("Book");//二级节点
//为二级节点添加属性,属性值为对应属性的值
secondRoot.addElement("no").setText(book.getNo() + "");
secondRoot.addElement("name").setText(book.getName() + "");
secondRoot.addElement("value").setText(book.getValue() + "");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter writer = new XMLWriter(new FileOutputStream("D:\\book.xml"), format);
writer.write(document);
writer.close();
document.clearContent();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized List<Book> readXML() {
List<Book> list = new ArrayList<Book>();//创建list集合
File file = null;
try {
file = new File("D:\\book.xml");//读取文件
if (file.exists()) {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
List nodeList = document.selectNodes("Books/Book");
for (int i = 0; i < nodeList.size(); i++) {
Element el = (Element) nodeList.get(i);
Book book = new Book();
book.setNo(Integer.parseInt(el.elementText("no")));
book.setName(el.elementText("name"));
book.setValue(Double.parseDouble(el.elementText("value")));
list.add(book);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
class Test {
public static void main(String args[]) {
BookList bl = new BookList();
boolean bBreak = true;
while (bBreak) {
System.out.println("请输入操作代码:");
System.out.println("1:添加 2:删除 3:修改 4:查询 5:书籍统计 6:退出");
Scanner sc = new Scanner(System.in);
int code = sc.nextInt();
if (code == 1) {
System.out.println("请输入编号");
int no = sc.nextInt();
System.out.println("请输入书名");
String name = sc.next();
System.out.println("请输入售价");
double value = sc.nextDouble();
Book book = new Book(no, name, value);
bl.add(book);
bl.writeXmlDocument(book);
} else if (code == 2) {
System.out.println("请输入要删除的书籍名");
String name = sc.next();
if (bl.delete(name)) {
System.out.println("删除成功");
} else {
System.out.println("书籍不存在");
}
} else if (code == 3) {
System.out.println("请输入要修改的书籍名");
String name = sc.next();
Book bookBefore = bl.query(name);
System.out.println("请输入新的编号");
int newNo = sc.nextInt();
System.out.println("请输入新的书名");
String newName = sc.next();
System.out.println("请输入新的售价");
double value = sc.nextDouble();
Book bookAfter = new Book(newNo, newName, value);
bl.update(bookBefore, bookAfter);
} else if (code == 4) {
System.out.println("请输入要查询的书籍名");
String name = sc.next();
Book book = bl.query(name);
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue());
} else if (code == 5) {
List<Book> list = bl.getBookList();
System.out.println("总书籍数:" + bl.getCount());
for (Book book : list) {
System.out.println("编号:" + book.getNo() + " 书名:" + book.getName() + " 售价:" + book.getValue());
}
} else if (code == 6) {
bBreak = false;
}
}
}
}
jar 包 dom4j.jar jaxen-1.1.4.jar