2016-11-02 7 views
0

MySQLからレコードを読み込んでコードを解析し、XMLファイルに保存するコードを記述しました。今、私が持っているJava DOM XML Parser - 印刷されたXMLに複数のタグの代わりに1つのタグが含まれています

<part><row>.....</row><row>...</row></part> 

:ここ

<part><row>...</row></part> 

は私poroblemを示す画面である:

enter image description here

ここでは、Javaコードセクションがこれは何を達成したいことは、このようなsomethinましたXMLにデータをパーズするDocument構造体:

private void callSPInParOrWithout(final Document doc, 
      final Connection conn) { 
     ResultSet rs = null; 
     CallableStatement cs = null; 
     try { 
      // <part> 
      Element part = doc.createElement("part"); 
      doc.appendChild(part); 

      cs = conn.prepareCall("{CALL getBrandRows(?)}"); 
      cs.setString(1, "Brand#13"); 

      boolean results = cs.execute(); 
      while (results) { 
       // <row> 
       Element row = doc.createElement("row"); 
       part.appendChild(row); 
       rs = cs.getResultSet(); 
       while (rs.next()) { 
        // <p_partkey> 
        Element pPartKey = doc.createElement("p_partkey"); 
        pPartKey.appendChild(doc.createTextNode(Integer.toString(
          rs.getInt("p_partkey")))); 
        row.appendChild(pPartKey); 
        // <p_name> 
        Element pName = doc.createElement("p_name"); 
        pName.appendChild(doc.createTextNode(rs.getString(
          "p_name"))); 
        row.appendChild(pName); 
        // <p_mfgr> 
        Element pMfgr = doc.createElement("p_mfgr"); 
        pMfgr.appendChild(doc.createTextNode(rs.getString(
          "p_mfgr"))); 
        row.appendChild(pMfgr); 
        // <p_brand> 
        Element pBrand = doc.createElement("p_brand"); 
        pBrand.appendChild(doc.createTextNode(rs.getString(
          "p_brand"))); 
        row.appendChild(pBrand); 
        // <p_type> 
        Element pType = doc.createElement("p_type"); 
        pType.appendChild(doc.createTextNode(rs.getString(
          "p_type"))); 
        row.appendChild(pType); 
        // <p_size> 
        Element pSize = doc.createElement("p_size"); 
        pSize.appendChild(doc.createTextNode(Integer.toString(
          rs.getInt("p_size")))); 
        row.appendChild(pSize); 
        // <p_container> 
        Element pContainer = doc.createElement("p_container"); 
        pContainer.appendChild(doc.createTextNode(rs.getString(
          "p_container"))); 
        row.appendChild(pContainer); 
        // <p_retailprice> 
        Element pRetailPrice = doc.createElement("p_retailprice"); 
        pRetailPrice.appendChild(doc.createTextNode(
          Float.toString(rs.getFloat("p_retailprice")))); 
        // <p_comment> 
        Element pComment = doc.createElement("p_comment"); 
        pComment.appendChild(doc.createTextNode(rs.getString(
          "p_comment"))); 
        row.appendChild(pComment); 
       } 
       results = cs.getMoreResults(); 
      } 
     } catch (SQLException e) { 
      JOptionPane.showMessageDialog(null, e.getMessage(), 
        "Exception occured", JOptionPane.ERROR_MESSAGE); 
     } finally { 
      try { 
       if (rs != null) rs.close(); 
       if (cs != null) cs.close(); 
      } catch (SQLException e) { 
      } 
     } 
    } 

スクリーンショットでわかりましたが、それはうまくいきますが、私は<row>というタグを作成して間違いました。

答えて

1

row要素は、ResultSetのすべての行を反復処理する前に1回だけ作成します。

期待される結果を得るには、ループ内にrow要素を作成し、​​をそのループのボディの末尾に移動します。

CallableStatement#execute()は、結果セットが使用可能かどうかを示します。 CallableStatement#getMoreResults()は、別の結果セットが使用可能かどうかを示します。あなたのステートメントが複数の結果セットを返さない限り、それを呼び出す際には使用できません(this answerも参照)。あなたのCallableStatementが1つの結果セットしか返さない場合は、whileの代わりにifを安全に使用し、getMoreResults()への呼び出しを取り除くことができます。

関連する問題