Wednesday, 13 December 2017

java - Sax Parser Missing opening Element

itemprop="text">

Im having an issue opening the
stackoverflow posts XML dump using the java sax parser. It recognizes the end of each
element but seems to skip the startElement method. Im using the example
code:




 try
{

SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser saxParser =
factory.newSAXParser();

DefaultHandler handler = new
DefaultHandler() {

public void startElement(String uri, String
localName, String qName,
Attributes attributes) throws SAXException
{


System.out.println("Start Element :" +
qName);

}

public void endElement(String uri,
String localName,
String qName) throws SAXException {


System.out.println("End Element :" + qName);



}

public void characters(char ch[], int start, int length) throws
SAXException {

}

};


saxParser.parse(filename,
handler);



The
XML is structured simply as a row with a number of
attributes:



            "postTypeID=1"
AcceptedAnswer...>


and
the output from the code above is
simply:



End Element
:row



I have
tried adding @Override annotations to the methods inside the defaultHandler however it
throws an error if i try to override the startElement. Can anyone recommend how to
correct this issue and why startElement cannot be overriden?



Answer




If you get an error when adding an
Override annotation, the most likely explanation is that the
signature of your method does not quite match the one of the method you intend to
override.



From the href="https://docs.oracle.com/javase/8/docs/api/org/xml/sax/helpers/DefaultHandler.html"
rel="nofollow noreferrer">Java 8 JavaDocs of the SAX
DefaultParser:



public
void startElement(String uri,
String localName,
String
qName,

Attributes attributes)
throws
SAXException


this
looks ok, but possibly you are using a different version.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...