处理来自 HttpRequest 的错误 XML
我的 XML 解析器有问题 - 我通过以下方式收到 XML:
public String executeHttp() {
try {
post.setEntity(new UrlEncodedFormEntity(nvp));
resp = client.execute(post);
HttpEntity ent = resp.getEntity();
return EntityUtils.toString(ent, "UTF-8");
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
然后将其交给解析器:
Xml.parse(this.executeHttp(), this);
它工作得很好,它解析了我需要的内容,天哪,生活很棒......但是。 我刚刚发现我在真实设备上遇到了一些严重的异常,这些设备的互联网连接不是很好,因为我的解析器无法处理诸如“我没有得到正确的 XML”之类的情况。 ,“那个 XML 并没有真正通过。”或者“呃……这个 XML 真的没有任何意义。”
我相信它必须在解析器中的某个地方。有什么好的建议应该把这个“XML验证”放在哪里吗?
多谢!
I have a problem with XML parser - I receive a XML via:
public String executeHttp() {
try {
post.setEntity(new UrlEncodedFormEntity(nvp));
resp = client.execute(post);
HttpEntity ent = resp.getEntity();
return EntityUtils.toString(ent, "UTF-8");
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
and then give it to the parser:
Xml.parse(this.executeHttp(), this);
which works just fine, it parses what I need and gosh, life is great...however.
I have just found out that I get some serious exceptions on real devices, where the internet connection isn't that great thanks to the fact that my parser isn't handling situations such as: "I didn't get a proper XML.", "That XML didn't make it really through." or "Uhm..this XML doesn't make any sense, really."
I believe it has to be somewhere in the parser. Any good advices where this "XML verification" should be placed?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了!它一直就在我面前!
我没有删除整个应用程序,而是捕获异常并重复下载 XML 文件直到完成。
Problem solved! It was right in front of me the whole time!
Instead of taking down the whole app, I'm catching an exception and repeating downloading the XML file until it's complete.
您需要的是根据 XML 架构文件(xsd 文档)验证您从 HTTP 请求中收到的 XML 数据。鉴于您以前从未这样做过,我将此处开始并首先创建一个有效的架构,然后然后继续 Java 验证部分。
What you're looking for is to validate the XML data you received from your HTTP request against an XML Schema file (xsd document). Seeing as you've never done this before, I would start here and first create a valid schema, and then move on to the Java validation portion.