为什么列表用新对象替换旧对象

时间:2014-05-28 14:38:15

标签: java arraylist jaxb

我正在尝试使用JAXB通过xml读取对象并更新一些保存信息。 这是读取和更新代码 -

dbf = DocumentBuilderFactory.newInstance();
dbuilder = dbf.newDocumentBuilder();
document = dbuilder.parse(file);
jc = JAXBContext.newInstance(OutletWisePlanningListContainer.class);
Binder<Node> binder = jc.createBinder();
owpLContainer = (OutletWisePlanningListContainer)     
binder.unmarshal(document);
owpLContList = owpLContainer.getOwpList();

然后 - 更新对象

for (OutletWisePlanningList owpl1 : owpLContList) {
    owpl = owpl1.getOwpList();
    owpList = new OutletWisePlanningList();
    owpList = owpl1;
    skuList.add(owpl1.getSkuId());
    for (i = 1; i < sku; i++) {
        if (owpl1.getSkuId().trim().equals(request.getParameter("skuId" + i).trim())) {
            owpl1.getSkuId();
            ArrayList idList = new ArrayList();
            int j = 1, cnt = 1;
            al1.clear();
            int perf = Integer.parseInt(request.getParameter("hdnPerf" + i));
            for (OutletWisePlanning owps : owpl) {
                owp = new OutletWisePlanning();
                for (j = 1; j < perf; j++) { //used only when Planned outlets is changed
                    if (owp.getMarketIntId().trim().equals(request.getParameter("UID" + i + '-' + j).trim()) && !request.getParameter("txtTARGET_SETvolume" + i + '-' +  j).trim().equals("0")) {
                        //some updation in owp object
                        al1.add(owp);    
                    }
                }
                if (request.getParameter("selPlanUnplanned").equals("0")) { //used when Unplanned in    selected
                    al1.add(owp);
                }
            }
            owpList.setOwpList(al1);
            owpList.setSkuId(owpl1.getSkuId());
        }
    }
    owpList1.add(owpList);
    Iterator itr = owpList1.iterator();
    while (itr.hasNext()) {
        OutletWisePlanningList op1 = (OutletWisePlanningList) itr.next();
        for (OutletWisePlanning op2 : op1.getOwpList()) {
            System.out.println("Party id in the owpList1" + op2.getPartyId());
        }
    }   
}
al.addAll(owpList1);

从我正在读取数据并更新相同的xml -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OutletWisePlanningListContainer>
<OutletWisePlanningList skuId="4214">
    <OutletWisePlanning partyId="14560">
        <marketIntId>14002</marketIntId>
        <perfAllocId>818</perfAllocId>
        <relPartyName>Aangan (Indian North)</relPartyName>
        <addrLine1>Bhikaji Cama Place</addrLine1>
        <locationName>-NA-</locationName>
        <planQty>30</planQty>
        <planCpc>10</planCpc>
        <agreedQty>0</agreedQty>
        <agreedCpc>0</agreedCpc>
        <executedQty>0</executedQty>
        <executedCpc>0</executedCpc>
        <actualQty>0</actualQty>
        <actualCpc>0</actualCpc>
        <claimedQty>0</claimedQty>
        <claimedCpc>0</claimedCpc>
        <genCpc>0</genCpc>
        <eligibility></eligibility>
        <eligibleItem>0</eligibleItem>
    </OutletWisePlanning>
</OutletWisePlanningList>
<OutletWisePlanningList skuId="4215">
    <OutletWisePlanning partyId="14554">
        <marketIntId>14105</marketIntId>
        <perfAllocId>819</perfAllocId>
        <relPartyName>Dhaba (Indian North)</relPartyName>
        <addrLine1>Aurangzeb Road</addrLine1>
        <locationName>Aurangzeb Road</locationName>
        <planQty>44</planQty>
        <planCpc>10</planCpc>
        <agreedQty>0</agreedQty>
        <agreedCpc>0</agreedCpc>
        <executedQty>0</executedQty>
        <executedCpc>0</executedCpc>
        <actualQty>0</actualQty>
        <actualCpc>0</actualCpc>
        <claimedQty>0</claimedQty>
        <claimedCpc>0</claimedCpc>
        <genCpc>0</genCpc>
        <eligibility></eligibility>
        <eligibleItem>0</eligibleItem>
    </OutletWisePlanning>
</OutletWisePlanningList>

我正在使用不同的skuId(即ok)获取xml,但partyId = 14560正在被partyId = 14554替换为其整个数据。 请帮忙

1 个答案:

答案 0 :(得分:0)

此代码包含许多不必要的初始化和分配。你应该清除所有这些。 然而,为了回答你的问题,我最好的猜测是问题在于for for循环。

for (OutletWisePlanning owps : owpl) {
            owp = new OutletWisePlanning();
            for (j = 1; j < perf; j++) { //used only when Planned outlets is changed
                if (owp.getMarketIntId().trim().equals(request.getParameter("UID" + i + '-' + j).trim()) && !request.getParameter("txtTARGET_SETvolume" + i + '-' +  j).trim().equals("0")) {
                    //some updation in owp object
                    al1.add(owp);    
                }
            }
            if (request.getParameter("selPlanUnplanned").equals("0")) { //used when Unplanned in    selected
                al1.add(owp);
            }
    }

首先,你要在for-each循环中初始化owp。 owp = new OutletWisePlanning();永远不会在if条件下为您分配任何值。这里代码不完整或者你可能在构造函数中有一些初始化。无论如何,在内部for循环中,相同的对象参考&#34;当if条件(循环内部和外部)满足时,会被修改并添加到列表al1中。因此,无论您将对象添加到此列表中的次数,都将具有相同的值。