package com.change.data.read;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
//导入w3c的DOM接口和java的解析器对象
import com.change.data.domain.*;
import com.change.data.services.*;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ReadFromDBBook {
// 声明文档构建器工厂
private DocumentBuilderFactory dbf=null;
// 声明文档构建器
private DocumentBuilder db=null;
// 声明文档
private Document doc=null;
/**
* 构造函数实例化
*
* @throws ParserConfigurationException
*
*/
public ReadFromDBBook() throws ParserConfigurationException{
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
}
/**
*
*/
public void readAll(){
doc=db.newDocument();
ArrayList array1=new BookServices().selectBooksMySQL();
ArrayList array2=new BookServices().selectBooksSQL();
ArrayList array3=new BookServices().selectBooksOracle();
ArrayList array4=new BookServices().selectBooksAccess();
// 生成根元素
Element root=doc.createElement("图书信息:");
// 生成字元素
for (int i = 0; i < array1.size(); i++) {
Book book=(Book)array1.get(i);
Element bookid=doc.createElement("图书代号");
// 为book设置属性
bookid.setAttribute("id", book.getId()+"");
bookid.setAttribute("DataBase", "MySQL");
Element bookno=doc.createElement("图书编号");
// 为bookclass添加属性
bookno.appendChild(doc.createTextNode(book.getId()+""));
Element bookclass=doc.createElement("图书类型");
// 为bookclass添加属性
bookclass.appendChild(doc.createTextNode(book.getClassid()+""));
Element booktitle=doc.createElement("图书标题");
// 为booktitle添加属性
booktitle.appendChild(doc.createTextNode(book.getTitle()));
Element bookpaper=doc.createElement("内容简介");
// 为bookpaper添加属性
bookpaper.appendChild(doc.createTextNode(book.getPaper()));
Element booknumber=doc.createElement("图书编码");
// 为bookpaper添加属性
booknumber.appendChild(doc.createTextNode(book.getNumber()));
Element bookauthor=doc.createElement("图书作者");
// 为bookauthor添加属性
bookauthor.appendChild(doc.createTextNode(book.getAuthor()));
Element bookprice=doc.createElement("图书价格");
// 为bookprice添加属性
bookprice.appendChild(doc.createTextNode(book.getPrice()+""));
// 形成层次结构(将一系列属性添加到book对象中)
bookid.appendChild(bookno);
bookid.appendChild(booktitle);
bookid.appendChild(bookclass);
bookid.appendChild(bookpaper);
bookid.appendChild(booknumber);
bookid.appendChild(bookprice);
bookid.appendChild(bookauthor);
// 将student对象添加到根接点
root.appendChild(bookid);
}
for (int i = 0; i < array2.size(); i++) {
Book book=(Book)array2.get(i);
Element bookid=doc.createElement("图书代号");
// 为book设置属性
bookid.setAttribute("id", book.getId()+"");
bookid.setAttribute("DataBase", "SQL Server");
Element bookno=doc.createElement("图书编号");
// 为bookclass添加属性
bookno.appendChild(doc.createTextNode(book.getId()+""));
Element bookclass=doc.createElement("图书类型");
// 为bookclass添加属性
bookclass.appendChild(doc.createTextNode(book.getClassid()+""));
Element booktitle=doc.createElement("图书标题");
// 为booktitle添加属性
booktitle.appendChild(doc.createTextNode(book.getTitle()));
Element bookpaper=doc.createElement("内容简介");
// 为bookpaper添加属性
bookpaper.appendChild(doc.createTextNode(book.getPaper()));
Element booknumber=doc.createElement("图书编码");
// 为bookpaper添加属性
booknumber.appendChild(doc.createTextNode(book.getNumber()));
Element bookauthor=doc.createElement("图书作者");
// 为bookauthor添加属性
bookauthor.appendChild(doc.createTextNode(book.getAuthor()));
Element bookprice=doc.createElement("图书价格");
// 为bookprice添加属性
bookprice.appendChild(doc.createTextNode(book.getPrice()+""));
// 形成层次结构(将一系列属性添加到book对象中)
bookid.appendChild(bookno);
bookid.appendChild(booktitle);
bookid.appendChild(bookclass);
bookid.appendChild(bookpaper);
bookid.appendChild(booknumber);
bookid.appendChild(bookprice);
bookid.appendChild(bookauthor);
// 将student对象添加到根接点
root.appendChild(bookid);
}
for (int i = 0; i < array3.size(); i++) {
Book book=(Book)array3.get(i);
Element bookid=doc.createElement("图书代号");
// 为book设置属性
bookid.setAttribute("id", book.getId()+"");
bookid.setAttribute("DataBase", "Oracle");
Element bookno=doc.createElement("图书编号");
// 为bookclass添加属性
bookno.appendChild(doc.createTextNode(book.getId()+""));
Element bookclass=doc.createElement("图书类型");
// 为bookclass添加属性
bookclass.appendChild(doc.createTextNode(book.getClassid()+""));
Element booktitle=doc.createElement("图书标题");
// 为booktitle添加属性
booktitle.appendChild(doc.createTextNode(book.getTitle()));
Element bookpaper=doc.createElement("内容简介");
// 为bookpaper添加属性
bookpaper.appendChild(doc.createTextNode(book.getPaper()));
Element booknumber=doc.createElement("图书编码");
// 为bookpaper添加属性
booknumber.appendChild(doc.createTextNode(book.getNumber()));
Element bookauthor=doc.createElement("图书作者");
// 为bookauthor添加属性
bookauthor.appendChild(doc.createTextNode(book.getAuthor()));
Element bookprice=doc.createElement("图书价格");
// 为bookprice添加属性
bookprice.appendChild(doc.createTextNode(book.getPrice()+""));
// 形成层次结构(将一系列属性添加到book对象中)
bookid.appendChild(bookno);
bookid.appendChild(booktitle);
bookid.appendChild(bookclass);
bookid.appendChild(bookpaper);
bookid.appendChild(booknumber);
bookid.appendChild(bookprice);
bookid.appendChild(bookauthor);
// 将student对象添加到根接点
root.appendChild(bookid);
}
for (int i = 0; i < array4.size(); i++) {
Book book=(Book)array4.get(i);
Element bookid=doc.createElement("图书代号");
// 为book设置属性
bookid.setAttribute("id", book.getId()+"");
bookid.setAttribute("DataBase", "Access");
Element bookno=doc.createElement("图书编号");
// 为bookclass添加属性
bookno.appendChild(doc.createTextNode(book.getId()+""));
Element bookclass=doc.createElement("图书类型");
// 为bookclass添加属性
bookclass.appendChild(doc.createTextNode(book.getClassid()+""));
Element booktitle=doc.createElement("图书标题");
// 为booktitle添加属性
booktitle.appendChild(doc.createTextNode(book.getTitle()));
Element bookpaper=doc.createElement("内容简介");
// 为bookpaper添加属性
bookpaper.appendChild(doc.createTextNode(book.getPaper()));
Element booknumber=doc.createElement("图书编码");
// 为bookpaper添加属性
booknumber.appendChild(doc.createTextNode(book.getNumber()));
Element bookauthor=doc.createElement("图书作者");
// 为bookauthor添加属性
bookauthor.appendChild(doc.createTextNode(book.getAuthor()));
Element bookprice=doc.createElement("图书价格");
// 为bookprice添加属性
bookprice.appendChild(doc.createTextNode(book.getPrice()+""));
// 形成层次结构(将一系