• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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();
parser.setContentHandler(parserHandler);
parser.setErrorHandler(parserHandler);
// 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.
Md.Ajmal
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Bill
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic