acc15 acc15 - 24 days ago 10
Android Question

XmlPullParser resource management

How does XmlPullParser manage resources?
Why are there no

close()
,
release()
or some similar method?

For example I want to read XML from a file:

final XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
final XmlPullParser xpp = xppf.newPullParser();
xpp.setInput(new FileInputStream("test/file.xml));
while (xpp.next() // parsing...
// ...


Who should close this
FileInputStream
and release resources?

If
XmlPullParser
will close
FileInputStream
automagically after whole file has been read, what will happen if an exception is thrown or if I never call
xpp.nextXXX()
?

Will it close stream anyway, so I shouldn't worry about resource management?

Answer

You opened the file with

new FileInputStream("test/file.xml)

so you should close it. Also, you should be aware that XmlPullParser will throw exceptions.