You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Ranjith Kodikara <ra...@dmsswt.com> on 2004/11/05 07:27:57 UTC

Problem with SAX Parsing in Xerces

This isthe code I use to parse an xml inside a method of one of my java classes. 

         XMLReader xmlReader = null;

         try {

            SAXParserFactory spfactory = SAXParserFactory.newInstance();
            spfactory.setValidating(false);

            SAXParser saxParser = spfactory.newSAXParser();

            xmlReader = saxParser.getXMLReader();

    try {
     xmlReader.setFeature( "http://xml.org/sax/features/namespaces", true);
    }
    catch (SAXException e) {
     System.err.println("could not set parser feature");
    }
   SurveyReader t = new SurveyReader();
   xmlReader.setContentHandler(t);
   xmlReader.setErrorHandler(t);

   InputSource source = new InputSource(xmlSource);
   xmlReader.parse(source);

I am creating and populating Vectors in startElement, characters and endElement methods of this class. if I print contents of those vectors just after xmlReader.parse(source) 
line, it correctly displays the contents  of Vectors. That proves the XML is correctly parsed and Vectors are filled with the data. 

But what I am doing is, instantiating the class containing this code inside a method of another class. On that instance, I invoke the method containing above mentioned code snippet. After calling that method(The Vectors should have been filled now), I try to print contents of Vectors on that instance. eg. reffered as ins.VectName;. But at this point the Vectors are empty and not filled with contents of XML. 

(If I print the content of Vector inside the Class that's been instantiated, it diaplays the Vector contents accurrately. But when I am accessing the Vector( note that it is a public member defined as public Vector VectName;)  from the method which the called class is instantiated, the Vector is seen as empty.

I guess there is something in SAX that it quits from the class's method while its parsing the XML like. I dont know.
That's the problem I am facing. I appreciate and graetful for any help.

Thanks and best regards
Ranjith