Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.xml.sax.SAXParseException: Unexpected token while parsing XML

Update:- Seems like I have to give the URL which is returning me the xml. Here it is - URL


I am getting SAXParseException when parsing an xml. The xml I am trying to parse is as follows -

<?xml version="1.0" encoding="utf-8"?>
<markers xmlns="">
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
    <marker name="" address1="" address2="" region="" country="" zip="" imgsrc="" lat="" lng="" distance="" />
</markers>

The code which I am using to parse this xml is as follows -

public Document getDomElement(String xml) {
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        Log.e("ParserConfigurationException: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        e.printStackTrace();
        Log.e("SAXException: ", e.getMessage());
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("IOException: ", e.getMessage());
        return null;
    }
    return doc;
}

I am using same code to parse other xml data and it works. I couldn't understand what's there in this xml data which is stopping it to parse the data properly. I have checked the xml with many xml validators online too.

I must be missing something obvious here.

EDIT:- The xml I am trying to parse is from a webservice(the name, address etc. are not actually empty). When I try to use the xml directly by hardcoding in the code itself, it works fine. But when trying to use it directly from webservice response, is not working for me.

The exception:-

10-09 20:39:50.328: W/System.err(16211): org.xml.sax.SAXParseException: Unexpected token (position:TEXT @1:2 in java.io.StringReader@41941010) 
10-09 20:39:50.335: W/System.err(16211):    at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.WebService.XMLDataParser.getDomElement(XMLDataParser.java:159)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller.processResult(APICaller.java:386)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller.access$1(APICaller.java:385)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller$SoapCaller.onPostExecute(APICaller.java:361)
10-09 20:39:50.335: W/System.err(16211):    at com.blackcobrastudios.ukash.Manager.APICaller$SoapCaller.onPostExecute(APICaller.java:1)
10-09 20:39:50.335: W/System.err(16211):    at android.os.AsyncTask.finish(AsyncTask.java:602)
10-09 20:39:50.343: W/System.err(16211):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-09 20:39:50.343: W/System.err(16211):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-09 20:39:50.343: W/System.err(16211):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 20:39:50.343: W/System.err(16211):    at android.os.Looper.loop(Looper.java:137)
10-09 20:39:50.343: W/System.err(16211):    at android.app.ActivityThread.main(ActivityThread.java:4536)
10-09 20:39:50.343: W/System.err(16211):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 20:39:50.351: W/System.err(16211):    at java.lang.reflect.Method.invoke(Method.java:511)
10-09 20:39:50.351: W/System.err(16211):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-09 20:39:50.351: W/System.err(16211):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-09 20:39:50.351: W/System.err(16211):    at dalvik.system.NativeStart.main(Native Method)
10-09 20:39:50.351: E/SAXException:(16211): Unexpected token (position:TEXT @1:2 in java.io.StringReader@41941010) 
like image 263
noob Avatar asked Jan 13 '23 04:01

noob


1 Answers

The problem was in the encoding. The following answer explains it properly -

https://stackoverflow.com/a/2869127/1079901

I just have to call this regex on the response string -

response = response.replaceAll("[^\\x20-\\x7e]", "");

Thanks to BalusC for such a perfect answer.

like image 68
noob Avatar answered Jan 27 '23 22:01

noob