Two Laptop Bag*
The moose likes XML and Related Technologies and the fly likes Values stored in member variable(ArrayList) of parser are getting lost after parsing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Values stored in member variable(ArrayList) of parser are getting lost after parsing." Watch "Values stored in member variable(ArrayList) of parser are getting lost after parsing." New topic
Author

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

Mohammed Ajmal
Greenhorn

Joined: Sep 20, 2004
Posts: 29
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Values stored in member variable(ArrayList) of parser are getting lost after parsing.