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 01-07 00:23:58.895: WARN/System.err(221): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse( 01-07 00:23:58.906: WARN/System.err(221): at javax.xml.parsers.DocumentBuilder.parse( 01-07 00:23:58.915: WARN/System.err(221): at com.paad.earthquake.Earthquake.refreshEarthquakes( 01-07 00:23:58.925: WARN/System.err(221): at com.paad.earthquake.Earthquake.onCreate( 01-07 00:23:58.936: WARN/System.err(221): at 01-07 00:23:58.936: WARN/System.err(221): at 01-07 00:23:58.946: WARN/System.err(221): at 01-07 00:23:58.955: WARN/System.err(221): at$2200( 01-07 00:23:58.966: WARN/System.err(221): at$H.handleMessage( 01-07 00:23:58.976: WARN/System.err(221): at android.os.Handler.dispatchMessage( 01-07 00:23:58.987: WARN/System.err(221): at android.os.Looper.loop( 01-07 00:23:58.996: WARN/System.err(221): at 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( 01-07 00:23:59.040: WARN/System.err(221): at$ 01-07 00:23:59.046: WARN/System.err(221): at 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) {

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 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'. >$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) | Android