/*
DOM解析轻松入门(二)--简单的DOM实战
author:shine
*/
以下这个例子,使用xml充当数据库。完成增删改查。
一、准备:
1)需要在“DOM解析轻松入门(一)”中的DOMSerializer类。
2)准备一个xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
<book id="1">
<name>Chinese</name>
<description>ChineseBook</description>
</book>
<book id="2">
<name>math</name>
<description>mathBook</description>
</book>
<book id="3">
<name>english</name>
<description>englishBook</description>
</book>
</books>
二、代码:
1)创建一个实体类Book:
package com.shine.vo;
public class Book {
private String bookId;
private String bookName;
private String description;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
2)使用DAO类BookDAO 进行增、删、改、查
package com.shine.dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.shine.util.DOMSerializer;
import com.shine.vo.Book;
public class BookDAO {
private String xmlURI = "D://workplace//test1.xml";
private DOMParser parser;
private Document doc;
private Element root;
public BookDAO() {
try {
DOMParser parser = new DOMParser();
BufferedReader br = new BufferedReader(new FileReader(xmlURI));
InputSource inputSource = new InputSource(br);
parser.parse(inputSource);
doc = parser.getDocument();
root = doc.getDocumentElement();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//主函数中试验一下,(所有方法已经测试过,由于篇幅原因,就测试findAllBooks方法,其他方法就不一一演示了,大家可以试一下)
public static void main(String[] args) {
//使用findAllBooks方法
BookDAO dao = new BookDAO();
ArrayList list = dao.findAllBooks();
for(int i=0; i<list.size(); i++) {
Book book = (Book)list.get(i);
System.out.println("id="+book.getBookId());
System.out.println("name="+book.getBookName());
System.out.println("description="+book.getDescription());
}
}
// 按编号查找book,返回null表示此book不存在, !null表示查找成功。
public Book findOneBook(String bookId) {
Book book = null;
Element rightBook = (Element) this.findRightBook(bookId);
if (rightBook != null) {
book = new Book();
String bookName = rightBook.getElementsByTagName("name").item(0)
.getTextContent();
String description = rightBook.getElementsByTagName("description")
.item(0).getTextContent();
book.setBookId(bookId);
book.setBookName(bookName);
book.setDescription(description);
}
return book;
}
// 插入book,返回0表示此book已存在,1表示插入成功
public int insertBook(Book book) {
int flag = 0;
Element rightBook = (Element) this.findRightBook(book.getBookId());
if (rightBook == null) {
// 增加book元素
Element bookNode = doc.createElement("book");
bookNode.setAttribute("id", book.getBookId());
root.appendChild(bookNode);
// 增加bookName元素
Element bookNameNode = doc.createElement("name");
Text bookNameText = doc.createTextNode(book.getBookName());
bookNameNode.appendChild(bookNameText);
bookNode.appendChild(bookNameNode);
// 增加bookDescription
Node bookDescriptionNode = doc.createElement("description");
Text bookDescriptionText = doc
.createTextNode(book.getDescription());
bookDescriptionNode.appendChild(bookDescriptionText);
bookNode.appendChild(bookDescriptionNode);
this.save();
flag = 1;
}
return flag;
}
// 删除book,返回0表示此book不存在,1表示删除成功
public int removeBook(String bookId) {
int flag = 0;
Node node = this.findRightBook(bookId);
if (node != null) {
root.removeChild(node);
flag = 1;
}
this.save();
return flag;
}
//更新book,返回0表示此book不存在,1表示更新成功
public int updateBook(Book book) {
int flag = 0;
Element rightBook = (Element) this.findRightBook(book.getBookId());
if (rightBook != null) {
rightBook.getElementsByTagName("name").item(0).setTextContent(
book.getBookName());
rightBook.getElementsByTagName("description").item(0)
.setTextContent(book.getDescription());
this.save();
flag = 1;
}
return flag;
}
//查找所有的书籍
public ArrayList findAllBooks() {
ArrayList bookList = new ArrayList();
NodeList nodeList = root.getElementsByTagName("book");
for(int i=0; i<nodeList.getLength(); i++) {
Book book = new Book();
Element bookNode = (Element)nodeList.item(i);
String bookId = bookNode.getAttributes().getNamedItem("id").getNodeValue();
String bookName = bookNode.getElementsByTagName("name").item(0).getTextContent();
String description = bookNode.getElementsByTagName("description").item(0).getTextContent();
book.setBookId(bookId);
book.setBookName(bookName);
book.setDescription(description);
bookList.add(book);
}
return bookList;
}
//根据bookId找到正确的book节点
public Node findRightBook(String bookId) {
Node rightBook = null;
NodeList nodeList = doc.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
String id = nodeList.item(i).getAttributes().getNamedItem("id")
.getNodeValue();
if (id != null && id.equals(bookId)) {
rightBook = nodeList.item(i);
break;
}
}
return rightBook;
}
// 存入文件
public void save() {
try {
FileWriter fw = new FileWriter(xmlURI);
BufferedWriter bw = new BufferedWriter(fw);
DOMSerializer serializer = new DOMSerializer();
serializer.serialize(doc, bw);
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
DOM解析轻松入门(三)--.DOM Level 2 Tranversal 和Range 2008-2-17
*/