XML/SAX - endElement is never triggered

Discussion in 'Java' started by =?UTF-8?B?UsOpbWk=?= Cocula, Apr 25, 2005.

  1. 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 ?


    }
     
    =?UTF-8?B?UsOpbWk=?= Cocula, Apr 25, 2005
    #1
    1. Advertising

  2. =?UTF-8?B?UsOpbWk=?= Cocula

    shakah Guest

    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Ă©mi Cocula wrote:
    > 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 ?
    >
    >
    > }
     
    shakah, Apr 25, 2005
    #2
    1. Advertising

  3. =?UTF-8?B?UsOpbWk=?= Cocula

    Remi COCULA Guest

    "shakah" <> wrote in message news:<>...
    > 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
     
    Remi COCULA, Apr 26, 2005
    #3
  4. =?UTF-8?B?UsOpbWk=?= Cocula

    steph Guest

    Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
    > "shakah" <> wrote in message news:<>...
    >
    >>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


    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.

    --
    stephane
    retirez les lettres majuscules et le 666 de l'adresse pour l'utiliser.
     
    steph, Apr 26, 2005
    #4
  5. =?UTF-8?B?UsOpbWk=?= Cocula

    steph Guest

    Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
    > "shakah" <> wrote in message news:<>...
    >
    >>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


    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.

    --
    stephane
    retirez les lettres majuscules et le 666 de l'adresse pour l'utiliser.
     
    steph, Apr 26, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Scheffler

    JAXP:SAX: EntityResolver never used

    Thomas Scheffler, Nov 12, 2003, in forum: XML
    Replies:
    0
    Views:
    635
    Thomas Scheffler
    Nov 12, 2003
  2. Soren Kuula
    Replies:
    1
    Views:
    455
    Henry S. Thompson
    Dec 1, 2005
  3. Kevin
    Replies:
    4
    Views:
    429
    Irrwahn Grausewitz
    Oct 17, 2003
  4. Thomas
    Replies:
    4
    Views:
    197
    DJ WIce
    Jan 27, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    463
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page