I’m making an application in which I have to do XML parsing.
I have to check if the user is able to access new data only then I have to delete the old data in the database.
For the above I am using the following code:
try{
URL url = new URL(address);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader */
xr.setContentHandler(this);
/* Parse the xml-data from our URL. */
InputSource is = new InputSource(url.openStream());
//once data is obtained then delete the table.
hb.executeSql("DELETE FROM Products,Category");
xr.parse(is);
}
catch{
e.printstacktrace();
}
If there is an error in the input stream then an exception will be thrown which I’m catching and the code for deleting the table will never be executed.
Is the logic correct?
You are almost there, if url.OpenStream() throws an exception you would catch it. But to be completely on the safe side, you need to put
before
Because xr.parse could throw an IO or SAX exception, and your code would delete even if that happens.
for API, please see
http://download.oracle.com/javase/1.5.0/docs/api/org/xml/sax/XMLReader.html#parse(org.xml.sax.InputSource)
Also i would recommend you catch, IO and SAX exceptions seperately. It would help you debug things easily.