The SAX parser fails for the following HTML data
<article class="episode">
<img class="lazy-loaded" src="./index_files/timthumb(3).php" data-src="https://example.com/test.jpg&w=390&h=210&zc=1">
</img>
<a href="abc"></a>
</article>
but works well when i remove the tag as shown below:
<article class="episode">
<img class="lazy-loaded" src="./index_files/timthumb(3).php" data-src="https://example.com/test.jpg&w=390&h=210&zc=1">
</img>
</article>
Following is the DefaultHandler written for parsing
import android.util.Log;
import com.barnowlanims.starlight.MovieListItem;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
public class SaxXmlMovieListHandler extends DefaultHandler {
private final String FILENAME="SaxXmlMovieListHandler";
private List<MovieListItem> mMovieListItems;
private String tempVal;
private MovieListItem movieListItem;
private boolean isArticleStarted=false, isSectionStarted=false;
public SaxXmlMovieListHandler() {
mMovieListItems = new ArrayList<>();
}
public List<MovieListItem> getMovieListItems() {
return mMovieListItems;
}
// Event Handlers
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal="";
Log.e(FILENAME, "startElement: qName:"+qName+", localName:"+localName+", uri"+uri);
if (qName.equalsIgnoreCase("article") && attributes.getValue("class").equalsIgnoreCase("episode")) {
// create a new instance of MovieListItem
movieListItem = new MovieListItem();
isArticleStarted = true;
}else if (qName.equalsIgnoreCase("section") || attributes.getValue("class").contains("cardBox")) {
movieListItem = new MovieListItem();
isSectionStarted = true;
}else if (qName.equalsIgnoreCase("img")) {
if(isArticleStarted) {
movieListItem.setThumbnail(attributes.getValue("data-src"));
}
}else if (qName.equalsIgnoreCase("a")) {
if(isArticleStarted) {
movieListItem.setUri(attributes.getValue("href"));
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.e(FILENAME, "endElement: qName:"+qName+", localName:"+localName+", uri"+uri);
if (qName.equalsIgnoreCase("article")) {
if(isArticleStarted){
mMovieListItems.add(movieListItem);
isArticleStarted = false;
}
// add it to the list
} else if (qName.equalsIgnoreCase("section")) {
if(isSectionStarted){
mMovieListItems.add(movieListItem);
isSectionStarted = false;
}
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
Log.e(FILENAME, "qName:"+"endDocument");
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
Log.e(FILENAME, "qName:"+"startDocument");
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
super.fatalError(e);
Log.e(FILENAME, "qName:"+e.getStackTrace());
}
@Override
public void error(SAXParseException e) throws SAXException {
super.error(e);
Log.e(FILENAME, "qName:"+e.getStackTrace());
}
}
A snippet of error log when running with tag
E/SaxXmlMovieListHandler: qName:startDocument
E/SaxXmlMovieListHandler: startElement: qName:article, localName:article, uri
E/SaxXmlMovieListHandler: startElement: qName:img, localName:img, uri
E/SaxXmlMovieListHandler: endElement: qName:img, localName:img, uri
E/SaxXmlMovieListHandler: startElement: qName:a, localName:a, uri
D/SaxXmlParser: parse() failed[Ljava.lang.StackTraceElement;@425b5ab0
E/BlankFragment: Parser returned null
I would really appreciate some help