MrBoolean
MrBoolean

Reputation: 75

Why is the startElement method not being accepted?

I am testing out the SaxParser and I overwrote the DefaultHandler and its methods! For some reason I can not override the method "startElement". It doesn't event start. I tested it out with a syso command. Has anybody a solution for my problem? Thank you in advance!

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.jar.Attributes;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Parserusw {

    static LinkedList<String>list = new LinkedList<String>();

    public static void main(String[]args) throws ParserConfigurationException, SAXException{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler(){


        @Override
          public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException{
            if(qName.equals("Start"))
                Parserusw.list.add("Start");
            Parserusw.list.add("Start1");
            Parserusw.list.add("Start2");
            System.out.println("uidwwd");

        }
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String text;
            text = String.valueOf(ch, start, length);

            if(text.equals("Ende"))
                Parserusw.list.add("Ende");


        }
        @Override
        public void endElement (String namespaceURI,String localName, String qName){
            for(String s:Parserusw.list){
                System.out.println(s);
            }
            System.out.println("uidwwd");
        }
    };

    try {
        saxParser.parse(new File("c:\\Users\\User\\GP\\Arbeit\\src\\iwasmitparser\\test.xml"),handler);
    } catch (IOException e) {

        e.printStackTrace();
    }



}

}

Upvotes: 0

Views: 248

Answers (1)

Michael Kay
Michael Kay

Reputation: 163468

You've imported the wrong Attributes class:

import java.util.jar.Attributes;

It should be the one from org.xml.sax.

Upvotes: 1

Related Questions