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 ?
}
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 ?
}