4

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

4 回答 4

4

问题出在编码中。以下答案正确解释了它 -

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

我只需要在响应字符串上调用这个正则表达式 -

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

感谢BalusC提供如此完美的答案。

于 2013-10-10T14:59:13.357 回答
2

我建议放弃 sax 解析器并使用 Android 官方支持的XMLPullParser。Android Gingerbread (2.3.3) 之后不支持 Sax 解析器

于 2013-10-09T15:01:08.463 回答
2


如果XML包含Unicode字符,例如阿拉伯文波斯文字母,StringReader则会例外。在这些情况下,将InputStream直接传递给Document对象

于 2014-09-30T08:27:48.037 回答
1

我建议在通过代码运行程序之前尝试在程序中加入一些 XSD 验证。

以下内容可能会对您的工作有所帮助:

于 2013-10-09T15:06:43.287 回答