?? xmlconn.java
字號:
package dboperate;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* <p>Title:勿忘軟件,lzquan </p>
*
* <p>Description:勿忘軟件 </p>
*
* <p>Copyright: 泉水依然 Copyright (c) 2007-03-20</p>
*
* <p>Company: 泉水依然</p>
*
* @author :李政權,湖南農業大學科學技術師范學院04計算機教育班.
*
* QQ:25241418
*/
public class xmlConn{
private DocumentBuilderFactory domfac;
private DocumentBuilder dombuilder;
public Document doc;
public Element root;
private NodeList notForget;
private int nodeNumber;//當前共有多少節點.
private int columnNum;
private Object[][] data;
private int rowCount;
/**
* 構造函數:
* @param fileName xml文件名
* @param columnNum 文件字段的個數.
*/
public xmlConn(String fileName,int columnNum){
this.domfac = DocumentBuilderFactory.newInstance();
this.columnNum = columnNum;
try {
this.dombuilder = domfac.newDocumentBuilder();
InputStream is = null;
//FileInputStream 從文件系統中的某個文件中獲取輸入字節
if (fileName.equals("Not_Forget")) {
is = new FileInputStream("data/Not_Forget.xml");
} else if (fileName.equals("Class_Name")) {
is = new FileInputStream("data/Class_Name.xml");
}
this.doc = dombuilder.parse(is);
this.root = doc.getDocumentElement();
this.notForget = root.getChildNodes();
this.nodeNumber = notForget.getLength();
} catch (ParserConfigurationException ex) {
} catch (FileNotFoundException ex1) {
} catch (IOException ex2) {
} catch (SAXException ex2) {
}
}
/**
* 讀取xml文檔,以二維數組返回所有數據.
*/
public Object[][] getData(){
int row = notForget.getLength();
int arryRow =(int)(row / 2);//總記錄條數.
this.rowCount = arryRow;
int arryColumn=this.columnNum;
int m=0,k=0;
//根據文件類型確定行數.
//定義object類型二維數組 Data.
Object[][] Data = new Object[arryRow][arryColumn];
if (this.notForget != null) {
for (int i = 0; i < row; i++) {
Node notForgetMemoire = notForget.item(i);
for (Node node = notForgetMemoire.getFirstChild(); node != null;
node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Data[m][k]=node.getFirstChild().getNodeValue();
k++;
if(k/arryColumn==1){
m++;
k=0;
}
}
}
}
}
this.data = Data;
return Data;
}
/**
* 取得最終顯示在jtable中的數據.
* @param data Object : xml中所有的數據.
* @return Object[][] : 數組返回.
*/
public Object[][] getAllList(Object data){
Object[][] tableData = new Object[this.rowCount][5];
for(int i=0;i<this.data.length;i++)
for(int j=0;j<5;j++){
tableData[i][j] = this.data[i][j];
}
return tableData;
}
/**
* 分類讀取xml文檔.
*/
public Object[][] getData(int classID) {
return null;
}
/**
*返回xml文檔中最大的id值.
*/
public int getMaxID(){
Object[][] arry = this.data;
int MaxID=1;
for(int a=0;a<arry.length;a++){
int thisId = Integer.parseInt(arry[a][0].toString());
if(thisId > MaxID){
MaxID = thisId;
}
}
return MaxID;
}
/**
* 返回節點個數.
* @return int
*/
public int getRowCount(){
return this.rowCount;
}
public boolean writXml(Object[][] data){
try {
writexml insert = new writexml();
insert.toWrite(data);
insert.toSave();
return true;
} catch (ParserConfigurationException ex) {
return false;
}
}
public static void main(String args[]) {
xmlConn xml = new xmlConn("Not_Forget",9);
xml.getData();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -