org.xml.sax.SAXParseException: Unexpected token wh

2019-02-18 09:04发布

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) 

4条回答
成全新的幸福
2楼-- · 2019-02-18 09:07


If the XML contains Unicode characters such as arabic or Persian letters, StringReader would make an exception. In these cases, pass the InputStream straightly to the Document object

查看更多
何必那么认真
3楼-- · 2019-02-18 09:18

I recommend abandoning the sax parser and using the Android officially supported XMLPullParser. Sax parser is not supported after Android Gingerbread (2.3.3)

查看更多
家丑人穷心不美
4楼-- · 2019-02-18 09:27

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-02-18 09:32

I would recommend trying to incorporate some XSD validation in your program before you run it through your code.

The following may help you in your efforts:

查看更多
登录 后发表回答