Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > XML/SAX - endElement is never triggered

Reply
Thread Tools

XML/SAX - endElement is never triggered

 
 
=?UTF-8?B?UsOpbWk=?= Cocula
Guest
Posts: n/a
 
      04-25-2005

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 ?


}
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      04-25-2005
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 ?
>
>
> }


 
Reply With Quote
 
 
 
 
Remi COCULA
Guest
Posts: n/a
 
      04-26-2005
"shakah" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) roups.com>...
> 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
 
Reply With Quote
 
steph
Guest
Posts: n/a
 
      04-26-2005
Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
> "shakah" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) roups.com>...
>
>>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.
 
Reply With Quote
 
steph
Guest
Posts: n/a
 
      04-26-2005
Le 26/04/2005 09:36, Remi COCULA a &eacute;crit :
> "shakah" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) roups.com>...
>
>>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
You've never seen it before and you'll never see it again. Fred A Stover Computer Support 7 12-26-2007 03:33 AM
XML Schema never-never occurence of declared elements / attributes Soren Kuula XML 1 12-01-2005 01:27 PM
iframe height problem & oncontextmenu event never triggered Thomas Javascript 4 01-27-2004 09:36 AM
string routines go to never never land on unix Kevin C Programming 4 10-17-2003 06:07 PM
Re: Win98 CD is NOT bootable! never was-never will be :-) Andrew Tang A+ Certification 0 07-05-2003 08:23 AM



Advertisments