当前位置: 开源爱好者 » 编程教程 » Java教程 » 正文

Java 打开 读取xml文档内容,以二维数组返回数据.

Java读取xml文档,以二维数组返回所有数据.在代码中将根据文件类型确定行数.定义object类型二维数组 Data,取得最终显示在jtable中的数据:

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;
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();
    }
}
相关内容:

CopyRight 2015© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图