本文介绍了org.xml.sax.SAXParseException:解析XML意外标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新: - 好像我得给它回到我的XML的URL。这里是 - <一个href="http://applications.ukash.com/WhereToBuy/Markers.aspx?lat=53.34234&lng=-2.003&count=5&units=mi&cc=GB&l=partnername&search=Macclesfield"相对=nofollow>网址


我收到的SAXParseException解析XML时。我试图解析XML如下 -

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;标记的xmlns =&GT;
    &LT;标记名称=地址1 =地址2 =区域=国家=邮编=imgsrc =土地增值税=LNG =距离=/&GT;
    &LT;标记名称=地址1 =地址2 =区域=国家=邮编=imgsrc =土地增值税=LNG =距离=/&GT;
    &LT;标记名称=地址1 =地址2 =区域=国家=邮编=imgsrc =土地增值税=LNG =距离=/&GT;
    &LT;标记名称=地址1 =地址2 =区域=国家=邮编=imgsrc =土地增值税=LNG =距离=/&GT;
    &LT;标记名称=地址1 =地址2 =区域=国家=邮编=imgsrc =土地增值税=LNG =距离=/&GT;
&LT; /标记&GT;
 

而我使用的解析这个XML的code是如下 -

 公开文件getDomElement(XML字符串){
    文档DOC = NULL;
    DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
    尝试 {

        DocumentBuilder的DB = dbf.newDocumentBuilder();

        InputSource的是=新的InputSource();
        is.setCharacterStream(新StringReader(XML));
        DOC = db.parse(是);

    }赶上(的ParserConfigurationException E){
        e.printStackTrace();
        Log.e(的ParserConfigurationException:,e.getMessage());
        返回null;
    }赶上(的SAXException E){
        e.printStackTrace();
        Log.e(SAXException中:,e.getMessage());
        返回null;
    }赶上(IOException异常E){
        e.printStackTrace();
        Log.e(IOException异常:,e.getMessage());
        返回null;
    }
    返回文档;
}
 

我使用同样的code来解析其他的XML数据和它的作品。我不明白什么是有在该XML数据被停止它正确地分析数据。我检查了XML有许多XML验证网上了。

我必须缺少明显的东西在这里。

编辑: - 我试图解析XML是一个Web服务(名称,地址等,实际上不是空的)。当我尝试直接硬编码在code本身使用XML,它工作正常。但是,试图直接使用它从web服务的响应时,是不是为我工作。

的异常: -

  10-09 20:39:50.328:W / System.err的(16211):org.xml.sax.SAXParseException:意外令牌(位置:TEXT @ 1:2的java .io.StringReader @ 41941010)
10-09 20:39:50.335:W / System.err的(16211):在org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
10-09 20:39:50.335:W / System.err的(16211):在com.blackcobrastudios.ukash.WebService.XMLDataParser.getDomElement(XMLDataParser.java:159)
10-09 20:39:50.335:W / System.err的(16211):在com.blackcobrastudios.ukash.Manager.APICaller.processResult(APICaller.java:386)
10-09 20:39:50.335:W / System.err的(16211):在com.blackcobrastudios.ukash.Manager.APICaller.access $ 1(APICaller.java:385)
10-09 20:39:50.335:W / System.err的(16211):在com.blackcobrastudios.ukash.Manager.APICaller $ SoapCaller.onPostExecute(APICaller.java:361)
10-09 20:39:50.335:W / System.err的(16211):在com.blackcobrastudios.ukash.Manager.APICaller $ SoapCaller.onPostExecute(APICaller.java:1)
10-09 20:39:50.335:W / System.err的(16211):在android.os.AsyncTask.finish(AsyncTask.java:602)
10-09 20:39:50.343:W / System.err的(16211):在android.os.AsyncTask.access $ 600(AsyncTask.java:156)
10-09 20:39:50.343:W / System.err的(16211):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:615)
10-09 20:39:50.343:W / System.err的(16211):在android.os.Handler.dispatchMessage(Handler.java:99)
10-09 20:39:50.343:W / System.err的(16211):在android.os.Looper.loop(Looper.java:137)
10-09 20:39:50.343:W / System.err的(16211):在android.app.ActivityThread.main(ActivityThread.java:4536)
10-09 20:39:50.343:W / System.err的(16211):在java.lang.reflect.Method.invokeNative(本机方法)
10-09 20:39:50.351:W / System.err的(16211):在java.lang.reflect.Method.invoke(Method.java:511)
10-09 20:39:50.351:W / System.err的(16211):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
10-09 20:39:50.351:W / System.err的(16211):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-09 20:39:50.351:W / System.err的(16211):在dalvik.system.NativeStart.main(本机方法)
10-09 20:39:50.351:E /的SAXException:(16211):意外令牌(位置:TEXT @ 1:2 java.io.StringReader@41941010)
 

解决方案

但问题是在编码。以下的答案必须解释它正确 -

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

我只需要调用此正则表达式的响应字符串 -

 响应= response.replaceAll([^ \\ X20  -  \\ x7e],);
 

由于 BalusC 这样一个完美的答案。

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)
解决方案

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

http://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.

这篇关于org.xml.sax.SAXParseException:解析XML意外标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:34
查看更多