Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

Mohammed Ajmal
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(" noNamespaceSchemaLocation","test.xsd");
// parser.setFeature("", 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.
William Brogden
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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!