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)


Asked by: Kristian891 | Posted: 25-01-2022






Answer 1

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();
}

Answered by: Edward878 | Posted: 26-02-2022



Similar questions

java - SAXParseException when trying to convert String to Document

I'm parsing this XML Document, but getting the : org.xml.sax.SAXParseException: Unexpected token (position:TEXT Parameter not sp...@1:# in java.io.InputStreamReader@4124af00) I have tried


android - Parsing XML results in SAXParseException with Unexpected End of Document

I am running into a strange error with trying to parse XML from an android device. What is strange is parsing the xml worked several days before and I have not touched the code. Today, loading the xml will not work. The xml files are located in the "assets" folder in my project directory. Here is the part where I am passing the path of that specific xml file into a parser called MazeFileReader


java - Android Studio - SAXParseException when accessing wikipedia page

I have a very unusual issue with Android Studio, which started 4 days ago (i assume after the update). The problem is: The exception is thrown when trying to read (parse) any page, whether it is wikipedia, google, yahoo, some java documentation..., but when i write the same code in Eclipse, everything is working fine. So, here is the code from the calling class (activity): `new Thread(new ImgCrawlerTh...


android - Manifest merger exception: SAXParseException

We have a project that uses different product flavors and product types. When I select a particular flavor, this error is generated: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processSomeFlavorDebugManifest'. > com.android.manifmerger.ManifestMerger2$MergeFailureException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allo...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top