【发布时间】:2014-06-03 19:09:34
【问题描述】:
我正在学习解析 XML,在我使用它的教程中,我使用的是 saxParser。不幸的是,有些东西不起作用。不知道哪里出了问题。
代码 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) { }
}
代码 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);
}
所以我在手机的 sdcard 上有文件 .xml。在 LogCat 我得到
06-03 14:57:15.603: W/XML parsin(6578): you choose option
用于选择菜单中的按钮 但我无法到达
Log.w("XML parsin", "starting parsing document");
消息,因此这意味着应用程序无法打开我的 .xml 文件。
有人知道问题出在哪里吗?
【问题讨论】:
-
我必须补充一点,我仔细观看了一个教程,并且我已经按照视频中的方式完成了所有操作。对于视频中的人来说,一切都很好。
-
您确定您的路径适合您的 SD 卡吗?我总是看到类似stackoverflow.com/questions/13707647/file-path-from-sd-card 之类的东西。