【问题标题】:Java DOM XML Parser - printed XML contains one tag instead of multiple onesJava DOM XML Parser - 打印的 XML 包含一个标签而不是多个标签
【发布时间】:2016-11-02 04:39:19
【问题描述】:

我已经编写了一些从 MySQL 加载记录并解析它们并保存到 XML 文件中的代码。我想要实现的是这样的:

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

现在我有:

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

这是显示我的问题的屏幕:

这是将数据解析为 XML Document 结构的 Java 代码部分:

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) {
            }
        }
    }

正如您在屏幕截图中看到的那样,它运行良好,但我在创建 &lt;row&gt; 标记时犯了一些错误。

【问题讨论】:

    标签: java xml dom xml-parsing transformation


    【解决方案1】:

    在遍历 ResultSet 中的所有行之前,您只创建了一次 row 元素。

    要获得预期结果,请在while (rs.next()) 循环内创建row 元素,并将part.appendChild(row); 移动到该循环主体的末尾。

    CallableStatement#execute() 指示结果集是否可用。 CallableStatement#getMoreResults() 表示是否有另一个结果集可用;除非您的语句返回多个结果集,否则调用它是没有用的(另请参阅this answer)。如果您的 CallableStatement 只返回一个结果集,您可以安全地使用if 代替外部while,并删除对getMoreResults() 的调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多