<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA涂鴉
    關于JAVA的點點滴滴
    posts - 50,  comments - 689,  trackbacks - 0
    用jdom技術將數據庫數據寫入讀出xml文件

    //很多時候,為了避免多次訪問/查詢數據庫重的數據或者便于察看,將需要的數據一次取出并寫入xml文件

    //通過查詢條件m_condition 按照xmlMapPath的模式/模板 將從庫中的數據寫到resultXml
    //并返回記錄條數

    public int writeXML(String m_condtion,String xmlMapPath,String resultXml){
    int recordNum=0;
    String tableName = "table";
    String tableCol = "*";
    String sql = "select " + tableCol + " from " + tableName + " where " + m_condtion;

    Document mapDoc = null;
    Document dataDoc = null;
    Document newDoc = null;
    //開始準備工作
    try {
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    //Create the DocumentBuilder
    DocumentBuilder docbuilder = dbfactory.newDocumentBuilder();
    //Parse the file to create the Document
    mapDoc = docbuilder.parse(xmlMapPath);

    dataDoc = docbuilder.newDocument();
    //Instantiate the new Document
    newDoc = docbuilder.newDocument();
    } catch (Exception e) {
    System.out.println(e.getMassege());
    }

    //開始讀取映射作用的template文件

    Element mapRoot = mapDoc.getDocumentElement();
    Node dataNode = mapRoot.getElementsByTagName("data").item(0);
    Element dataElement = (Element)dataNode;

    //Create a new element called "data"
    Element dataRoot = dataDoc.createElement("data");

    try {
    /******** 這里創建一個連接conn,并創建statement
    根據不同的數據源創建
    ********************************/
    ResultSet resultset = stmt.query(sql);
    //Get the ResultSet information
    ResultSetMetaData resultmetadata = resultset.getMetaData();
    int numCols = resultmetadata.getColumnCount();
    Log.write("db_to_xml:numCols:" + numCols);


    while (resultset.next()) {
    //Create a new element called "row"
    Element rowEl = dataDoc.createElement("row");

    //為了便于瀏覽和讀,創建列數,以id為標記
    String colName = "id";
    String colVal =Integer.toString(++recordNum);
    Element dataEl = dataDoc.createElement(colName);
    dataEl.appendChild(dataDoc.createTextNode(colVal));

    rowEl.appendChild(dataEl);
    for (int i=1; i <= numCols; i++) {

    colName = resultmetadata.getColumnName(i);
    //Get the column value
    colVal = resultset.getString(i);

    //Determine if the last column accessed was null
    if (resultset.wasNull()) {
    colVal = "";
    }

    dataEl = dataDoc.createElement(colName);

    dataEl.appendChild(dataDoc.createTextNode(colVal));

    rowEl.appendChild(dataEl);
    }
    //Add the row to the root element
    dataRoot.appendChild(rowEl);
    }
    } catch (Exception e) {
    Log.write(e.getMessage());
    } finally {
    Log.write(" db_to_xml: Closing connections...");

    }

    //Add the root element to the document
    dataDoc.appendChild(dataRoot);


    Node node1= mapRoot.getElementsByTagName("root").item(0);
    Element newRootInfo =(Element)node1;
    Log.write("After got newRootInfo ...");
    //Retrieve the root and row information


    String newRootName = newRootInfo.getAttribute("name");

    String newRowName= newRootInfo.getAttribute("rowName");

    NodeList newNodesMap = mapRoot.getElementsByTagName("element");

    //Create the final root element with the name from the mapping file
    Element newRootElement=null;

    newRootElement = newDoc.createElement(newRootName);


    NodeList oldRows = dataRoot.getElementsByTagName("row");
    for (int i=0; i < oldRows.getLength(); i++){

    //Retrieve each row in turn
    Element thisRow = (Element)oldRows.item(i);

    //Create the new row
    Element newRow = newDoc.createElement(newRowName);

    for (int j=0; j < newNodesMap.getLength(); j++) {

    //For each node in the new mapping, retrieve the information
    //First the new information...
    Element thisElement = (Element)newNodesMap.item(j);
    String newElementName = thisElement.getAttribute("name");

    //Then the old information
    Element oldElement = (Element)thisElement.getElementsByTagName("content").item(0);
    String oldField = oldElement.getFirstChild().getNodeValue();


    //Get the original values based on the mapping information
    Element oldValueElement = (Element)thisRow.getElementsByTagName(oldField).item(0);
    String oldValue = oldValueElement.getFirstChild().getNodeValue();


    Element newElement = newDoc.createElement(newElementName);
    newElement.appendChild(newDoc.createTextNode(oldValue));


    NodeList newAttributes = thisElement.getElementsByTagName("attribute");
    for (int k=0; k < newAttributes.getLength(); k++) {
    //Get the mapping information
    Element thisAttribute = (Element)newAttributes.item(k);
    String oldAttributeField = thisAttribute.getFirstChild().getNodeValue();
    String newAttributeName = thisAttribute.getAttribute("name");

    oldValueElement = (Element)thisRow.getElementsByTagName(oldAttributeField).item(0);
    String oldAttributeValue = oldValueElement.getFirstChild().getNodeValue();


    newElement.setAttribute(newAttributeName, oldAttributeValue);
    }

    //Add the new element to the new row
    newRow.appendChild(newElement);
    }
    //Add the new row to the root
    newRootElement.appendChild(newRow);
    }
    //Add the new root to the document
    newDoc.appendChild(newRootElement);


    //把生成的xml文檔(newDoc)寫到文件中(路徑名為resultXml)
    try{
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    Properties properties = transformer.getOutputProperties();
    properties.setProperty(OutputKeys.ENCODING, "GB2312");//ISO8859_1,GB2312,UTF-8
    properties.setProperty(OutputKeys.METHOD, "xml");
    properties.setProperty(OutputKeys.VERSION, "1.0");
    properties.setProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperties(properties);

    DOMSource source = new DOMSource(newDoc);

    StreamResult result = new StreamResult(new java.io.File(resultXml));

    transformer.transform(source, result);
    //生成XML文件 完成
    } catch (Exception e) {
    System.out.println("XML file write:"+e.getMessage());
    }
    return recordNum;
    }
    posted on 2006-01-15 18:33 千山鳥飛絕 閱讀(621) 評論(0)  編輯  收藏 所屬分類: J2SE
    正在閱讀:



    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(35)

    隨筆檔案

    文章分類

    文章檔案

    好友的blog

    我的其他blog

    老婆的Blog

    搜索

    •  

    積分與排名

    • 積分 - 775104
    • 排名 - 56

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产成人免费全部网站| 亚洲国产精品激情在线观看| 亚洲电影国产一区| 99久久国产精品免费一区二区| 国产亚洲精品看片在线观看| 国产免费A∨在线播放| 日韩亚洲变态另类中文| 中文字幕在线免费视频| 亚洲大片在线观看| 国产91色综合久久免费| 在线亚洲高清揄拍自拍一品区| 在线观看视频免费国语| 精品国产亚洲一区二区三区在线观看| 又爽又高潮的BB视频免费看| eeuss影院免费直达入口| 国产亚洲成AV人片在线观黄桃| 免费看又黄又无码的网站| 亚洲依依成人精品| 四虎永久在线免费观看| 国产精品九九久久免费视频| 亚洲成在人线av| 在线观看AV片永久免费| 黄色免费在线观看网址| 亚洲av一综合av一区| 免费下载成人电影| 暖暖免费中文在线日本| 亚洲国产精品免费视频| 成熟女人牲交片免费观看视频| 国产精品亚洲精品日韩电影| 亚洲精品tv久久久久久久久| 亚洲成人免费网址| 真人无码作爱免费视频| 亚洲s色大片在线观看| 四虎成人免费网址在线| 99在线视频免费观看| 久久免费线看线看| 97久久国产亚洲精品超碰热| 亚洲av无码天堂一区二区三区| 免费A级毛片无码A∨| 美女被免费视频网站a| 亚洲视频一区调教|