gpt4 book ai didi

Java DOM XML 解析器 - 打印的 XML 包含一个标签而不是多个标签

转载 作者:行者123 更新时间:2023-12-01 09:18:10 24 4
gpt4 key购买 nike

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

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

现在我有:

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

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

enter image description here

这是将数据解析为 XML 的 Java 代码部分 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> 时犯了一些错误。标签。

最佳答案

在迭代 ResultSet 中的所有行之前,您仅创建一次 row 元素。

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

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

关于Java DOM XML 解析器 - 打印的 XML 包含一个标签而不是多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com