LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   android - how to access strings in a custom xml file in res/values? (http://www.linuxquestions.org/questions/programming-9/android-how-to-access-strings-in-a-custom-xml-file-in-res-values-4175436425/)

eantoranz 11-09-2012 11:32 AM

android - how to access strings in a custom xml file in res/values?
 
I have this file in res/values. Say it's X.xml where I have some strings inside:

Code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
    <!--
            wether we are on a release or not
            Values: true or false
    -->
    <string name="release">false</string>
    <string name="base_uri">http://blahblah</string>
   
    <!--  for testing purposes -->
    <string name="default_username">username</string>
    <string name="default_password">password</string>
   
</resources>

How can I access those values in code? Thanks in advance.

{BBI}Nexus{BBI} 11-09-2012 02:37 PM

You might get a quicker response if you post your question here: Android Questions. No need to register, log in using your LQ credentials.

David the H. 11-10-2012 02:02 PM

What do you even mean by "access", in terms of a smartphone? How do you expect to be able to extract the values, and what would you do with them? When you say "code", do you mean some programming language? Are you trying to create an app or something?


If this were a regular Linux system, with access to a shell and arbitrary programs, and you just needed some way to extract the text values, then I'd suggest something like xmlstarlet:

Code:

$ xmlstarlet sel -T -t -v '//string[@name="base_uri"]' -n file.xml
http://blahblah

But I have no idea what's possible on android. I agree that you should go to the androidquestions forums for this. And be sure to define your needs clearly.

eantoranz 11-10-2012 02:13 PM

I don't understand why you sound so ill-tempered. It's "native" android so we're talking about plain old java here.

In android there's a res/values/strings.xml file and you get the values contained inside with something as simple as (from inside one activity, for example):

Code:

getResources().getString(R.string.myValue);
R's members are kept in sync with the <string /> values you add to strings.xml automagically so it's very straight forward. Now I'd like to separate some of the values to another file and would like to be able to access the values contained there in (with the same ease would be very convenient).

schneidz 11-10-2012 02:35 PM

as a hax you can load something like connectbot or sshdroid and log into your fone and use regular commands like grep/sed/awk to parse the data you want.

eantoranz 11-13-2012 11:11 AM

Having the file not in res/values but in res/xml I was able to pull it off following this article: http://www.41post.com/4015/programmi...rom-a-xml-file

Cheers!


All times are GMT -5. The time now is 07:59 AM.