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

需要通过DOM java格式化xml的帮助吗,java,xml,dom,jdbc,Java,Xml,Dom,Jdbc,我正在尝试使用jdbc从数据库中获取数据,并根据结果创建xml。在我的数据库中,有一列具有重复值,但与重复值相对的值不同。 这是数据库的屏幕。。但当我试图用xml存储时,它会不断重复 这是我的密码: toDate=d1; fromDate=d2; Connection connection= null; Statement statement = null; ResultSet rp= null; S

我正在尝试使用jdbc从数据库中获取数据,并根据结果创建xml。在我的数据库中,有一列具有重复值,但与重复值相对的值不同。 这是数据库的屏幕。。但当我试图用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> 

老式汽车
80年代黑鹰直升机
红起动压铸
xyz
xyz
以下是我得到的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>

老式汽车
1941雪佛兰特级豪华敞篷车
Exoto设计
老式汽车
1937霍奇930V轿车
汽车艺术工作室设计
老式汽车
1928福特辉顿豪华轿车
66号公路迷你经典
我只想让产品线名称重复一次,而孩子重复多次,而不是让产品线名称和孩子都重复多次。
提前谢谢。

您会得到
产品线\u名称
及其
产品
子项重复相同次数,因为它们位于同一循环中:

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

相反,您需要为遇到的每个productline创建一个
product\u line\u name
元素,然后为您读取的每个数据库条目将
product
s附加到相应的
product\u line\u 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);
}
。。。
HashMap foundProductLineNames=新HashMap();
...
while(rp.next()){
字符串productLineName=rp.getString(“productLine”);
/*如果我们第一次看到这个产品线名称*/
如果(!FoundProductLineName.containsKey(productLineName)){
/*…我们必须为它创建一个DOM元素,并将其附加到产品集中*/
Element proLineNameElem=xmlDoc.createElement(“产品线名称”);
proLineNameElem.appendChild(xmlDoc
.createTextNode(rp.getString(“productLine”));
产品套装附件(proLineNameElem);
/*储存产品线以备日后使用*/
foundProductLineNames.put(productLineName,proLineNameElem);
}
/*继续使用原始代码添加产品,但使用
foundProductLineNames.get(productLineName)安装pro\u line\u name。
例如:*/
元素productElem=xmlDoc.createElement(“产品”);
foundProductLineNames.get(productLineName).appendChild(productElem);
}