0

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?

Widim
  • 7
  • 2
  • I must add that I watched a tutorial carefully and I have done everything exactly how it is done on the video. Still for the person on the video everything is working great. – Widim Jun 03 '14 at 19:13
  • Are you sure your path is right for your SD card? I have always seen something done like http://stackoverflow.com/questions/13707647/file-path-from-sd-card for it. – zgc7009 Jun 03 '14 at 19:19

1 Answers1

0

Two things need to be checked:

  1. Try file:///sdcard/osm.xml instead of file:///mnt/sdcard/osm.xml

  2. Did you miss READ_EXTERNAL_STORAGE permission in AndroidManifest.xml file?

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Community
  • 1
  • 1
Green goblin
  • 9,898
  • 13
  • 71
  • 100