How do I read strings.xml as a file?

I tried to access strings.XML, but I can't find the path to that. I need to read strings.XML

in the Values ​​folder as a file and access the contents of that. How can I access this file and what is its path?

Edit

I've used this before:

 Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields();
    String str = "";
    for (int  i =0; i < fields.length; i++) {
        int resId = ctx.getResources().getIdentifier(fields[i].getName(), "string", ctx.getPackageName());
        str += fields[i].getName() + " = ";
        if (resId != 0) {
            str += ctx.getResources().getString(resId);
        }
        str += "\n";
    }

      

but it returns all strings of all libraries, but I need the strings in the strings.xml file of my module.

I am writing Gradle code and I need things like this, but in java:

 def stringsFile = file("src/main/res/values/strings.xml")
def parsedProjectXml = (new XmlParser()).parse(stringsFile)

      

+3


source to share


4 answers


finally I found a solution with Gradle function and to the right of the build point, make a copy of strings.XML to a raw folder and parse that file after that.



0


source


I tried to access strings.xml but I cannot find its path.

There is no path because it is not a file on the device.

how can i access this file



You can not. You might be able to get the parser using an getXml()

object Resources

, although I haven't tried this for a string resource file.

what is his path?

He doesn't have it.

+2


source


You cannot read it out of the way. you can define an array of strings like this:

in XML

   <string-array name="arrayName">
    <item>@string/String1</item>
    <item>@string/String2</item>
    <item>@string/String3</item>
    <item>@string/String4</item>
    <item>@string/String5</item>

</string-array>

      

In java:

You can get the data like this:

       String[] stringsArray= getResources().getStringArray(R.array.arrayName);

        for (int i = 0; i < stringsArray.length; i++)
        {

        }   

      

+1


source


You cannot access strings.xml as an XML file and just read it. The values ​​from the resource files are referenced in the R.java file after the android application is built. Thus, you will have to access it using the Android API. Below is an example R.java file.

public final class R {  
    public static final class attr {  
    }  
    public static final class drawable {  
        public static final int ic_launcher=0x7f020000;  
    }  
    public static final class id {  
        public static final int menu_settings=0x7f070000;  
    }  
    public static final class layout {  
        public static final int activity_main=0x7f030000;  
    }  
    public static final class menu {  
        public static final int activity_main=0x7f060000;  
    }  
    public static final class string {  
        public static final int app_name=0x7f040000;  
        public static final int hello_world=0x7f040001;  
        public static final int menu_settings=0x7f040002;  
    }  
    public static final class style {  

        public static final int AppBaseTheme=0x7f050000;  



        public static final int AppTheme=0x7f050001;  
    }  
}

      

0


source







All Articles