开发者

Problem: ArrayList size increasing

开发者 https://www.devze.com 2023-02-21 21:51 出处:网络
hi am working with parsing xml file,i am returning ArrayList of class 开发者_如何学GoObjects, but for every call of this method, the size of the ArrayList is increasing from 0-8 and 8 to 16 and 24...

hi am working with parsing xml file,i am returning ArrayList of class 开发者_如何学GoObjects, but for every call of this method, the size of the ArrayList is increasing from 0-8 and 8 to 16 and 24... instead of creating a new ArrayList object.

Here is the code:

public ArrayList<clsCategory> ParseCategoryXml(String fileToParse)
{
    try 
    {
        System.out.println("before XML Pasing Array size is = " + categoryArray.size());

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Send URL to parse XML Tags */
            //URL sourceUrl = new URL("http://d.mobrobot.com/feeds/npv1/categories_Fox.xml");
            FileInputStream inputFile = new FileInputStream(fileToParse);
            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            xr.setContentHandler(this);
            xr.parse(new InputSource(inputFile));
    }
    catch (Exception e) 
    {
        System.out.println("XML Pasing Excpetion = " + e);
    }
    System.out.println("XML Pasing Array size is = " + categoryArray.size());
    return categoryArray;
}

Output in the Console for

XML Pasing Array size is = 8  //when First call of method
XML Pasing Array size is = 16  //when Secondcall of method

Can anyone explain me what's going on?


I don't think you are creating a new ArrayList anywhere in your code, so each time you run the parsing code, the parsed elements are appended to the old one.

Try something like:

categoryArray = new ArrayList<clsCategory>();

at the start of the method.


it looks like categoryArray is not local variable, it is an instance variable.. That is why its size increases for every call..

0

精彩评论

暂无评论...
验证码 换一张
取 消