【问题标题】:Need help to format xml through DOM java需要帮助通过DOM java格式化xml
【发布时间】:2019-11-18 21:12:35
【问题描述】:

我正在尝试使用 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 和孩子都重复多次。 提前谢谢你。

【问题讨论】:

    标签: java xml dom jdbc


    【解决方案1】:

    product_line_name 和它的 productchildren 重复相同的次数,因为它们在同一个循环中:

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

    相反,您需要为遇到的每个产品线创建一个product_line_name 元素,然后为您读取的每个数据库条目将products 附加到其对应的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);
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多