java - 需要帮助通过 DOM java 格式化 xml

标签 java xml dom jdbc

我正在尝试使用 jdbc 从数据库中获取数据并根据该结果创建 xml。在我的数据库中,我有一列具有重复值,但重复值的值不同。 这是数据库的屏幕。 db image 。但是当我尝试存储在 xml 中时,它会不断重复。

这是我的代码:

toDate=d1;
            fromDate=d2;
            Connection connection= null;
            Statement statement = null;
            ResultSet rp= null;
String query="SELECT p.productcode,p.productline,p.productvendor,p.productname,o.ordernumber,o.orderdate,
 t.ordernumber,t.productcode,t.quantityordered,t.priceeach,(t.quantityordered * t.priceeach) 
 as total from orders o join orderdetails t on o.ordernumber=t.ordernumber 
 join products p on p.productcode=t.productcode where orderdate
 between "?" and "?" order by productline;

PreparedStatement p2=connection.prepareStatement(query);
                p2.setString(1, toDate);
                p2.setString(2, fromDate);
rp=p2.executeQuery();

DOM 创建代码:

                        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();

                        DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

                        Document xmlDoc = documentBuilder.newDocument();

Element product_list=xmlDoc.createElement("product_list");
                        root.appendChild(product_list);
                        Element product_set=xmlDoc.createElement("productset");
                        product_list.appendChild(product_set);
                        while(rp.next()) {
                            Element pro_line_name=xmlDoc.createElement("product_line_name");
                            pro_line_name.appendChild(xmlDoc.createTextNode(rp.getString("productLine")));
                            product_set.appendChild(pro_line_name);

                            Element product=xmlDoc.createElement("product");
                            pro_line_name.appendChild(product);

                            Element pro_name=xmlDoc.createElement("product_name");
                            pro_name.appendChild(xmlDoc.createTextNode(rp.getString("productname")));
                            product.appendChild(pro_name);

                            Element pro_vendor=xmlDoc.createElement("product_vendor");
                            pro_vendor.appendChild(xmlDoc.createTextNode(rp.getString("productvendor")));
                            product.appendChild(pro_vendor);                
                        }
rp.close();
statement.close();
connection.close();

这是我正在寻找的 xml:

<product_list>  
<product_set>    
<product_line_name> vintage cars</product_line_name>   
<product>     
<product_name> 1980s Black Hawk Helicopter </product_name >     
<product_vendor> Red Start Diecast </product_vendor >      
</product>   
<prodcut> 
<product_name>xyz</product_name>
<pro_vendor>xyz</pro_vendor>
</product_set> 

这是正在获取的 xml:

 <product_list>
    <productset>
    <product_line_name>Vintage Cars<product>
    <product_name>1941 Chevrolet Special Deluxe Cabriolet</product_name>
    <product_vendor>Exoto Designs</product_vendor>
    </product>
    </product_line_name>
    <product_line_name>Vintage Cars<product>
    <product_name>1937 Horch 930V Limousine</product_name>
    <product_vendor>Autoart Studio Design</product_vendor>
    </product>
    </product_line_name>
    <product_line_name>Vintage Cars<product>
    <product_name>1928 Ford Phaeton Deluxe</product_name>
    <product_vendor>Highway 66 Mini Classics</product_vendor>
    </product>
    </product_line_name>

我只想要product_line_name一次,而子项重复多次,而不是我让product_line_name和子项都重复多次。 预先感谢您。

最佳答案

您会得到 product_line_name 及其 product 子项重复相同次数,因为您在同一循环中获取了它们:

while(rp.next()) { ... }

相反,您需要为遇到的每个产品线创建一个 product_line_name 元素,然后将 product 附加到其相应的 product_line_name 父级您阅读的每个数据库条目。

例如,您可以执行以下操作:

...
HashMap<String, Element> foundProductLineNames = new HashMap<String, Element>();
...

while (rp.next()) {
    String productLineName = rp.getString("productLine");
    /* If wee see this product line name for the first time... */
    if (!foundProductLineNames.containsKey(productLineName)) {
        /* ...we have to create a DOM element for it and append it to the product set. */
        Element proLineNameElem=xmlDoc.createElement("product_line_name");
        proLineNameElem.appendChild(xmlDoc
            .createTextNode(rp.getString("productLine")));
        product_set.appendChild(proLineNameElem);

        /* Store the product line for later use. */
        foundProductLineNames.put(productLineName, proLineNameElem);
    }
    /* Proceed with your original code for adding products but use
       foundProductLineNames.get(productLineName) instaed of pro_line_name.
       For example: */

    Element productElem=xmlDoc.createElement("product");
    foundProductLineNames.get(productLineName).appendChild(productElem);
}

关于java - 需要帮助通过 DOM java 格式化 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922975/

相关文章:

java - 将图像保存在 webcontent 的文件夹内

Java - 在图像中正确绘制多行字符串

android - 为什么这种布局不适用于 Android Studio?

Javascript XSLT 转换返回 null

javascript - 传递给 .attr() 的函数的索引和值参数

javascript - iframe 内的脚本标签在父 dom 上执行

Java 无法识别直线方程程序中的字符串 "-1"。查看 "equation"操作命令

java - Spring缓存不能与EHCache+JCache一起使用

java - 使用 Java 为 POS 创建自定义收据

javascript - 如何制作已从显示 :none to display:block available in the DOM? 更改的元素