• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Values stored in member variable(ArrayList) of parser are getting lost after parsing.

Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have created a class to read the xml file and which forms a value object and inserts into the ArrayList.

So, if the XML File has say 10 value objects information. Then It is creating 10 value objects and inserting into the arrayList successfully.

These I am printing in the endDocument() method of saxparser. It is printing successfully but once it comes after the
parser.parse(fileName); statement then the contents are getting lost from the arraylist. That is size of the arraylist is becoming 0.

The snippet of the code is :

import org.*;

public class ImportPersonalDetailsParser extends DefaultHandler
// holds the objects of person details...
ArrayList personsList = new ArrayList();


public void parseFile( File fileName )
// create a parser object
org.xml.sax.XMLReader parser = new org.apache.xerces.parsers.SAXParser();
//parser.setProperty("http://apache.org/xml/properties/schema/external- noNamespaceSchemaLocation","test.xsd");
// parser.setFeature("http://xml.org/sax/features/validation/schema", true);

// Instantiate this class to provide handlers for the parser and
// inform the parser about the handlers
ImportPersonalDetailsParser parserHandler = new ImportPersonalDetailsParser();
// Create an input source that describes the file to parse.
// then inform the parser to parse input from that source
org.xml.sax.InputSource input = new InputSource ( new FileReader ( fileName ) );
parser.parse( input );

//" Here size is shown as zero------------------"
System.out.println(" Size of personsList after parsing is....... : " + personsList.size());


... methods which form a personObject and insert into personsList....
working fine....

public endDocucment()
if ( personsList !=null ) && (personsList.size()> 0)
System.out.println("person name is ....");

In endDocument method it is printing details correctly but once it I am printing after the call to parser.parse(); it is showing the size as zero.. in the above statements....

I don;t know much about xml also don;t much documentation is given in xerces API. I think it is releasing the class object after the endDocument(). So if any one has faces such problem, could you explain why it is occuring and probable solution to retrieve the objects from arrayList.


Thanks & Regards.
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It looks to me like you are creating a new handler that is local to the parseFile method, rather than using the "this" handler which is still there after parseFile exits.
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic