cefobid
cefobid

Reputation: 21

SAXParseException in android

I am following the book "Professional Android Application Development" and trying to implement some examples. In chapter 5 page 151, the following code throws a SAXParseException. Does anyone know why?

Document dom = db.parse(in);

I tried other XML files, same exception was thrown.


Stack trace is like:

01-07 00:23:58.875: WARN/System.err(221): org.xml.sax.SAXParseException: expected: /meta read: head (position:END_TAG @1:87 in java.io.InputStreamReader@43d0c720) 01-07 00:23:58.895: WARN/System.err(221): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:151) 01-07 00:23:58.906: WARN/System.err(221): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:157) 01-07 00:23:58.915: WARN/System.err(221): at com.paad.earthquake.Earthquake.refreshEarthquakes(Earthquake.java:82) 01-07 00:23:58.925: WARN/System.err(221): at com.paad.earthquake.Earthquake.onCreate(Earthquake.java:60) 01-07 00:23:58.936: WARN/System.err(221): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-07 00:23:58.936: WARN/System.err(221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444) 01-07 00:23:58.946: WARN/System.err(221): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497) 01-07 00:23:58.955: WARN/System.err(221): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 01-07 00:23:58.966: WARN/System.err(221): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848) 01-07 00:23:58.976: WARN/System.err(221): at android.os.Handler.dispatchMessage(Handler.java:99) 01-07 00:23:58.987: WARN/System.err(221): at android.os.Looper.loop(Looper.java:123) 01-07 00:23:58.996: WARN/System.err(221): at android.app.ActivityThread.main(ActivityThread.java:4338) 01-07 00:23:58.996: WARN/System.err(221): at java.lang.reflect.Method.invokeNative(Native Method) 01-07 00:23:59.026: WARN/System.err(221): at java.lang.reflect.Method.invoke(Method.java:521) 01-07 00:23:59.040: WARN/System.err(221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 01-07 00:23:59.046: WARN/System.err(221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-07 00:23:59.056: WARN/System.err(221): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 4622

Answers (1)

James Goodwin
James Goodwin

Reputation: 7406

The parse method throws a SAXParseException in case there were any problems in parsing the XML that would cause the rest of the code not be be able to run, since if the XML is invalid then probably you don't want your code to continue.

The parse method declares that it throws SAXParseException so you must explicitly catch that exception when calling that method, e.g.

try {
    Document dom = db.parse(in);
} catch (SAXParseException e) {
    e.printStackTrace();
}

Upvotes: 2

Related Questions