Android xml saxparser not working



I have to extract some elements from XML read from network. Below class using to read.



public class SAXXMLParserThread extends ParserBaseThread implements Runnable,ParserBase{
private static final String TAG = "SAXXMLParser";
private Handler handler;
private SAXParser parser;
private Looper mLooper;
public DefaultHandler parseHandler;

public SAXXMLParserThread(Handler handler) {
// TODO Auto-generated constructor stub
// super(handler);
this.handler = handler;
Thread parserThread = new Thread(this);
parserThread.setName("com.example.saxparser");
parserThread.start();

}

@Override
public void run() {
// TODO Auto-generated method stub

SAXParserFactory factory=SAXParserFactory.newInstance();
try{
parser=factory.newSAXParser();

}catch(Exception ex){
Log.e(TAG,"Exception creating parser");
ex.printStackTrace();
}
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
// setPriority(mPriority);
// Process.setThreadPriority(mPriority);
Looper.loop();

}

// public class ParseHandler extends DefaultHandler{
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
Log.e(TAG,"DRC document end detected...");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
Log.e(TAG,"DRC document start detected...");

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
Log.e(TAG,"end tag received: </"+localName+">");

}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
Log.e(TAG,"DRC document start detected...");
}
// }

@Override
public void parseAndValidate(InputStream inputStream) {
// TODO Auto-generated method stub
if(parser != null){
try {
if(parseHandler == null)
parseHandler = new DefaultHandler();
Log.e(TAG,"calling parser.parse(inputStream, parseHandler );");
parser.parse(inputStream, parseHandler );
} catch (SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Looper getLooper() {
if (!Thread.currentThread().isAlive()) {
return null;
}
// If the thread has been started, wait until the looper has been
// created.
synchronized (this) {
while (Thread.currentThread().isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
// Log.e(TAG,"Returning looper asoicated with thread "+mLooper.getThread().getName());
return mLooper;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public boolean quitSafely(){
if(mLooper != null){
if(Build.VERSION.SDK_INT >=18)
mLooper.quitSafely();
else
mLooper.quit();
return true;
}
return false;

}
public void setHandler(Handler self) {
synchronized (this) {
this.handler = self;
}
}
}


I don't get any of the functions called...


If i set breakpoint and step into the parse method, i don;t have source attached, but in variable list i see some variables such as qName, localName, and the stream is actaully read, because the in adn out variables of the input stream are not 0 or -1. Also the xml coming is valid and the input stream is not waiting for data, i.e, the steam pushes adequate data.


I think, the XML is parsed, but the callbacks are not invoked..


Where am i making mistake,


Please help


No comments:

Post a Comment