I'm learning parsing XMLs and in the tutorial which I use it is by saxParser. Unfortunately something is not working. I don't know where is the problem.
Code MainActivity.java:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_parse) {
doParsing();
Toast.makeText(getBaseContext(), "lool", Toast.LENGTH_LONG).show();
Log.w("XML parsin", "you choose option");
return true;
}
return super.onOptionsItemSelected(item);
}
private void doParsing(){
URL urlRequest = null;
try {
urlRequest = new URL("file:///mnt/sdcard/osm.xml");
} catch (MalformedURLException e) {
}
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp;
XMLReader xr=null;
try {
sp = spf.newSAXParser();
xr = sp.getXMLReader();
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
}
myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
try { xr.parse(new InputSource(urlRequest.openStream())); }
catch (IOException e) { }
catch (SAXException e) { }
}
Code ExampleHandler.java:
package com.example.parsowanie;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
import android.widget.Toast;
public class ExampleHandler extends DefaultHandler {
public ExampleHandler() {
// TODO Auto-generated constructor stub
}
@Override
public void startDocument() throws SAXException {
Log.w("XML parsin", "starting parsing document");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}
So I have file .xml on the sdcard on the phone. In LogCat I get
06-03 14:57:15.603: W/XML parsin(6578): you choose option
which is for choose the button in the menu but I couldn't reach the
Log.w("XML parsin", "starting parsing document");
message so it means that application can't open my .xml file.
Anyone has an idea where is the issue?