0

I am attempting to localize my app so that it will display both Spanish and English. I have most of the localization figured out with one exception. I have an .xml file that lists questions for my app that I would like to move to a res/raw folder so that I can add locale and have the app translate.

For the most part this has been easy, as I've been able to call most strings from /res using (getResources().getString(R.string."") However I am having difficulty making this call for my current argument.

I have the xml located in res/raw/"".xml

and i am using asset manager at the moment to grab the .xml for the questions

 public static List<Question> readQuestions(AssetManager assetManager){

    ArrayList<Question> questionArrayList = new ArrayList<Question>();
    try{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(assetManager.open("questions.xml"));
        doc.getDocumentElement().normalize();
        NodeList nodeLst = doc.getElementsByTagName("question");
        for(int s=0; s<nodeLst.getLength(); s++){

Is anyone aware of a way I could make a call to the raw folder to grab the .xml?

EDIT:::

I am attempting to utilize code for inputstream, but i get an error at contexts that says cannot resolve symbol 'context'

 public static List<Question> readQuestions(AssetManager assetManager){

    ArrayList<Question> questionArrayList = new ArrayList<Question>();
    try{
        InputStream in = context.getResources().openRawResource(R.raw.questions);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document doc = builder.parse(in);


        NodeList nodeLst = doc.getElementsByTagName("question");
WiseGuy
  • 409
  • 1
  • 8
  • 19

1 Answers1

2

To get an InputStream for res/raw/questions.xml

InputStream in = context.getResources().openRawResource(R.raw.questions);

This answer assumes you have a Context. If you are not familiar with getting and using Context in Android I highly recommend taking the time to really understand it. You won't get far in Android programming otherwise.

There are a lot of answers on StackOverflow already discussing context. Here is one What is 'Context' on Android?

Community
  • 1
  • 1
slund
  • 6,367
  • 2
  • 26
  • 19
  • Alright, sorry i am pretty fresh to android programming, could you show me how i could implement this? – WiseGuy Dec 04 '12 at 20:23
  • For ways to get a usable context see http://stackoverflow.com/questions/3572463/what-is-context-in-android – slund Dec 05 '12 at 14:58