What's the best way to store and access XML in Android?

后端 未结 3 1448
执笔经年
执笔经年 2021-02-06 04:18

While I realize resources themselves are defined in XML files, if I have an XML file of my own type that I wish to use, should I be storing them in \"res/xml\"?

Is there

相关标签:
3条回答
  • 2021-02-06 04:44

    If you prefer ease of coding over lightning fast speed then I would use the Simple XML library instead. It is just easier to program for. I wrote a blog post that you can view that explains how to include it in a project of yours.

    0 讨论(0)
  • 2021-02-06 04:44

    First knowing how to correctly Access Files in Android:

    You might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID. Instead, you can save your resources in the assets/ directory.

    Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.

    However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource().

    Accessing XML Files

    http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml

    res/xml/
    
    Arbitrary XML files that can be read at runtime by calling Resources.getXML()
    

    Source

    Resources res = activity.getResources();
    XmlResourceParser xrp = res.getXml(R.xml.the_file_name_aka_resource_ID);
    
    0 讨论(0)
  • 2021-02-06 04:52

    I would go with adding the XML resource inside the res folder with the specific resource type. Its a convention that I have adapted to, having all my files in the same directory for organization.

    If you add your XML file inside of res/xml it can be accessed anytime at run-time via Resources.getXML()

    0 讨论(0)
提交回复
热议问题