XmlPullParser how to attain res/raw/xml/xmlfilename?

为君一笑 提交于 2019-12-02 06:16:55

XmlPullParser is not really a parser, it is an interface to a type of parser, called a "pull" parser.

The function getResources().getXml() returns an implementation of XmlPullParser for "parsing" XML resources. This is not a real XML parser -- in fact the original XML file was parsed at build time before being built into your app, and what this "XML parser" is doing is just returning the pre-digested XML structure as you call its API. This is the fastest "XML" parser available on Android (because it is not really parsing anything), but requires that the XML document be compiled as part of building your app.

The other implementation of XmlPullParser that you get from XmlPullParserFactory.newInstance() is not "limited" -- this implementation is full-featured, and can parse any raw XML document you give to it.

At least at one time (not sure if this is still the case), both the SAX parser and the parser returned by XmlPullParserFactory.newInstance() are actually built on the same underlying implementation, which is expat. The expat parser is a "push" parser (that is the same model as SAX), so the most efficient way to use it is with the SAX API. The XmlPullParser version has some more overhead from SAX since it needs to turn the underlying push semantics into a pull interface.

If it helps -- push means that it pushes the parsed data to you (callbacks you implement giving you each tag and other document element), while pull means you make calls to the parser to retrieve each element.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!