XML/SAX - endElement is never triggered

G

Guest

Hi,

I have a problem use the SAX API for parsing an XML file.

Here is a sample code :

-------------------------------------
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestSax {

private static class localSaxHandler extends DefaultHandler {

boolean labelState = false;

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("startElement : "+qName);
}

public void endElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("endElement : "+qName);
}

public void startDocument() {
System.out.println("startDocument");
}

public void endDocument() {
System.out.println("endDocument");
}

}

/**
* @param args
*/
public static void main(String[] args) {
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
parser.parse(new File("c:/tmp/toto.xml"),new localSaxHandler());

} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
--------------------

Here is a sample XML file :

---------------------
<?xml version="1.0" encoding="utf-8"?>
<racine>
<fils></fils>
<fils></fils>
</racine>
---------------------------

And then the output of the programm :

-------------------------------
startDocument
startElement : racine
startElement : fils
startElement : fils
endDocument
----------------------------------

The problem is that the endElement callback method is never invoked. Does
anyone see where it comes from ?


}
 
S

shakah

The endElement method should have the following signature:
public void endElement(String uri, String localName, String qName)
throws SAXException

Yours has an additional Attributes argument, so you get the endElement
method implemented by DefaultHandler. Remove that argument and you
should be good-to-go.
 
R

Remi COCULA

shakah said:
The endElement method should have the following signature:
public void endElement(String uri, String localName, String qName)
throws SAXException

Yours has an additional Attributes argument, so you get the endElement
method implemented by DefaultHandler. Remove that argument and you
should be good-to-go.

I am so stupid.
Thanks
 
S

steph

Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
I am so stupid.
Thanks

no!

but if you use a kind IDE like Eclipse, you will see in the margin of your
source code a green triangle if you override a method of the parent class. pass
your mouse on the triangle an you'll see which method is override.
 
S

steph

Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
I am so stupid.
Thanks

no!

but if you use a kind IDE like Eclipse, you will see in the margin of your
source code a green triangle if you override a method of the parent class. pass
your mouse on the triangle an you'll see which method is override.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top