J. Maes
J. Maes

Reputation: 6892

SAX Parser not working

I'm trying to read an xml file using sax in java. I only get output for the endelement but I can't find out what's wrong with the startElement.

This is my handler:

public class XMLHandler extends DefaultHandler {

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

public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
System.out.println( "SAX Event: END ELEMENT[ " + localName + " ]" );
}

}

I test it with the following code:

 public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
saxParser.parse("http://www.w3schools.com/xml/note.xml", new XMLHandler());
}

I use an example XML file from w3schools:

<!-- Edited by XMLSpy® -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

and this is my output:

SAX Event: END ELEMENT[ to ]
SAX Event: END ELEMENT[ from ]
SAX Event: END ELEMENT[ heading ]
SAX Event: END ELEMENT[ body ]
SAX Event: END ELEMENT[ note ]

I really don't get what I'm doing wrong here...

Upvotes: 0

Views: 1612

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

My guess is that your method prototypes are incorrect. this is why you should always use the @Override annotation. you probably imported the wrong Attributes class?

Upvotes: 3

Related Questions