-   Programming (
-   -   android - how to access strings in a custom xml file in res/values? (

eantoranz 11-09-2012 10: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:


<?xml version="1.0" encoding="utf-8"?>
            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>

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

{BBI}Nexus{BBI} 11-09-2012 01: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 01: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:


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

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 01: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):


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 01: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 10:11 AM

Having the file not in res/values but in res/xml I was able to pull it off following this article:


All times are GMT -5. The time now is 08:12 AM.