ParseException:在第 1 行,第 0 列:在 android 中找不到元素 SAXException
在我的一个 Android 应用程序中,当我尝试从某个 url 解析 xml 文件时,有时我会得到 ParseException: At line 1, column 0: no element found SAXException 但奇怪的是我没有得到这个异常总是意味着当我再次刷新页面时(返回到上一个活动并再次出现)它开始工作(意味着它解析 xml)。
我找不到发生这种情况的确切原因。在我看来,这可能是互联网的问题,但网络正在设备上运行,还有一件事它并不总是发生,有时它很容易地处理 XML,但有时它会抛出异常。
这是我用于解析的代码:
xr.parse(new InputSource(new URL(urlToHit).openStream()));
我也尝试了另一种解析方法:
URL serverAdd=new URL(urlToHit);
URLConnection connection = serverAdd.openConnection();
xr.parse(new InputSource(connection.getInputStream()));
但问题仍然存在。(有时它解析xml有时不能)
请帮助,因为它对我来说非常紧急。
谢谢。
In one of my android app when I am trying to parse an xml file from some url , sometimes i get ParseException: At line 1, column 0: no element found SAXException but the strange thing is I dont get this exception always means when I refresh the page again (going back to previous activity and coming again ) It starts working (means it parse the xml).
I could not find the exact reason why this is happening. It seems to me that It might be the problem of internet but the net is working on device and one more thing it does not happened always some time its prases the XML easily but sometimes it throws the exception.
Here is the code i am using for parsing :
xr.parse(new InputSource(new URL(urlToHit).openStream()));
I have tried another way also for parsing :
URL serverAdd=new URL(urlToHit);
URLConnection connection = serverAdd.openConnection();
xr.parse(new InputSource(connection.getInputStream()));
But the problem still continues.(Sometimes it parses the xml sometimes it cannot)
Please help as its very urgent for me .
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它与 Android 无关,也许您的互联网连接不可靠,因此 SAX 解析器无法获取有效的 XML 文件
It has nothing to do with Android, perhaps the internet connection you have is not reliable hence the SAX parser is not getting a valid XML file
首先看到这个问题谈论
问题代码和答案
是否有帮助。好吧,现在在这个问题中,我已将输入流的编码类型设置为 is.setEncoding("ISO-8859-1"); 在您的情况下也尝试一下。
First see this Question if it helps talking about the
Question code and Answer
both.Okay now in that question I have set encoding type to the inputstream as
is.setEncoding("ISO-8859-1");
try that in your case too.我建议不要使用 URLConnection..请参阅以下代码片段
它可以在没有 URLConnection 的情况下工作..尝试一下..
I suggest not use URLConnection..Refer follwing snippet
It works without URLConnection..Try it..