0

根据本网站上的建议,我采用了来自 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 的一致性吗?有人可以帮忙吗?

4

1 回答 1

1

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!

于 2011-05-18T13:21:20.143 回答