Android,simplexml serial.read 从空文件中读取问题
根据此站点上的建议,我采用了 org.simpleframework.xml 中的 SimpleXML。 我使用此代码从磁盘上的文件反序列化我的类:
try {
myPoints = serial.read(Points.class, new File(getFilesDir(), "points.xml"));
Log.i(TAG, "Number of Points: " + myPoints.getSize());
} catch (FileNotFoundException e) {
Log.d(TAG, "No data found!");
} catch (Exception e) {
Log.d(TAG, "Uncaught exception: ", e.getMessage());
}
如果文件“points.xml”的内容不是合法的 xml(在我的情况下它是一个空文件),serial.read 会中断(Persister 中发生异常) .class,抱歉我没有 simplexml 源...)。 我应该事先检查 xml 一致性吗? 有人可以帮忙吗?
Following suggestions on this site, I have adopted SimpleXML from org.simpleframework.xml.
I use this code to deserialize my class from a file on disk:
try {
myPoints = serial.read(Points.class, new File(getFilesDir(), "points.xml"));
Log.i(TAG, "Number of Points: " + myPoints.getSize());
} catch (FileNotFoundException e) {
Log.d(TAG, "No data found!");
} catch (Exception e) {
Log.d(TAG, "Uncaught exception: ", e.getMessage());
}
In the event the contents of file "points.xml" is not legal xml (in my case it's an empty file), serial.read breaks (an exception occurs in Persister.class, sorry I don't have simplexml sources...).
Should I check for xml consistency beforehand?
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需事先验证,因为您将无法解决问题。只需确保它正常失败(正如您的代码似乎正在做的那样)。
但是,在发生反序列化错误时,您可能想查看文件是否为空。空文件可能不是问题,而格式错误的 XML 文件才是问题!
No need to validate before-hand since you won't be able to fix the problem. Just make sure it fails gracefully (as your code appears to be doing).
You may, however, want to see if the file is empty or not in the case of a deserialization error. An empty file is likely not a problem where as a malformed XML file is!