0

I'm using a simple implementation of saxParser. Within my endElement method, I'm storing vo objects to an ArrayList. Unfortunately when I loop over my List, it only returns the last item from my xml data. Just wondering what I'm doing wrong? Relavent code below:

public class MyXMLHandler extends DefaultHandler {
    private StringBuffer buffer = new StringBuffer();
    private Boolean currentElement = false;

    private StoreDetails storeDetails = new StoreDetails(); //vo object
    private ArrayList<StoreDetails> dataList = new ArrayList<StoreDetails>(); //list of vo

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        currentElement = true;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        currentElement = false;     

        if (localName.equals("StoreID")) {
            buffer.toString().trim();
            storeDetails.setStoreId(buffer.toString());
        } else if (localName.equals("StoreName")) {
            buffer.toString().trim();
            storeDetails.setStoreName(buffer.toString());
        } else if (localName.equals("StoreCategory")) {
            buffer.toString().trim();
            storeDetails.setStoreCategory(buffer.toString());

            //add vo object to ArrayList - dataList
            dataList.add(storeDetails);
        }

        buffer = new StringBuffer();
    }    

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentElement) {
            buffer.append(ch, start, length);
            currentElement = false;
        }
    }

    @Override
    public void endDocument() throws SAXException {
        Log.i("TAG", "DONE PARSING XML");
        for(StoreDetails details : dataList){
            //ISSUE - returning only the last row in my xml data (over and over)
            Log.i("TAG", "Details ID: " + details.getStoreId());
        }
    }
}
worked
  • 5,762
  • 5
  • 54
  • 79

2 Answers2

1

You have to reinitialize the object after adding it to array list. Otherwise it saves the last data.

Now, For example , If your xml is something like this , then

<Item>
<StoreID></StoreID>
<StoreName></StoreName>
<StoreCategory></StoreCategory>
</Item>

At startElement you have to initialize the 'storeDetails' object.

storeDetails = new StoreDetails();

At endElement you have to add the 'storeDetails' object to the array list.

dataList.add(storeDetails);

In this way , when will occur at startElement , 'storeDetails' object will get initialize , it will save current item's information( StoreID,StoreName,StoreCategory ) & then when will occur at endElement , 'storeDetails' object will be added to the arraylist. Thus the parsing will go on and you will get all the data in your arraylist.

Junaid
  • 1,179
  • 2
  • 18
  • 35
  • Ok, this answer pointed me in the correct direction. To note, for my specific XML, I simply had to place the storeDetails = new StoreDetails(); within a certain localName conditional: if(localName.equals("Store")) storeDetails = new StoreDetails(); – worked Apr 09 '12 at 13:39
0

I guess the problem is that you use single instance of "vo object" for all items in ArrayList. You need smth like this in startElement:

storeDetailes = new ...

Refer to documents, explaning Java memory model.