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

问题描述 投票:0回答:1

我正在尝试使用jdbc从数据库中获取数据并根据该结果创建xml。在我的数据库中,我有一列包含重复值,但是相对于重复值的值是不同的。这是缺少db。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和child都多次重复。预先谢谢你。

java xml dom jdbc
1个回答
0
投票

[您使product_line_name及其子代product重复相同的次数,因为您使它们在同一循环中:

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

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

F.e。,您可以执行以下操作:

...
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.