Inflate XML file from internal memory

泪湿孤枕 提交于 2019-12-05 02:38:31

问题


I've copied an XML file from /assets to my applications data folder (data/data/package.name/files/). I'm doing this because the user will be able to modify a lot of data, and I want to save that data to the internal memory and then load it again when they restart the app. This all works well, using Root Browser I can see the XML file is properly copied to the data directory.

Now I need to inflate this XML file using a LayoutInflater. How would I access this file? With an XmlResourceParser or XmlPullParser?


回答1:


You cannot inflate that file using LayoutInflater. First, you can only inflate layout resources, not arbitrary files. Second, based on the description in your first paragraph, it is not a layout file in the first place.

If you want to parse arbitrary XML, use the DOM, SAX, or XmlPullParser.




回答2:


From the Android view API:

it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

A solution might be to pre-process XML files before. You must compress the XML file in the same way the Android SDK does at build time:

view inflation relies heavily on pre-processing of XML files that is done at build time

I guess you will have to call XmlPullParser.setInput to load your pre-processed XML, and then pass the XmlPullParser to View.inflate. I hope it is possible to do this kind of stuff even for content that is not in the APK.

I am very interested in any solution you can find. Please let me know if you find or create a prototype! This would be a decisive step towards creating a plugin architecture for my Open Source app.



来源:https://stackoverflow.com/questions/8197844/inflate-xml-file-from-internal-memory

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