Question

I developed an application Java Android and i have a problem :)

I want to load an XML file.

AssetManager assetManager = getAssets();
InputStream questionsFile = assetManager.open("questions.xml");
document = builder.parse(questionsFile);
NodeList nList = document.getElementsByTagName("question");

The XML is located in the Asset folder but i get an res Resources NotFoundException.

enter image description here

Thx for the help !

This is my XML file :

<questions>
    <question text='Qui est le réalisateur de blade runner ?'>
        <answer trueAnswer='false'>Stanley Kubrick</answer>
        <answer trueAnswer='false'>Steven Spielberg</answer>
        <answer trueAnswer='true'>Ridley Scott</answer>
        <answer trueAnswer='false'>Roland emmerich</answer>
    </question>
    <question text='Ou se situe le siège social de la société Facebook ?'>
        <answer trueAnswer='true'>Palo Alto</answer>
        <answer trueAnswer='false'>Philadelphie</answer>
        <answer trueAnswer='false'>Dallas</answer>
        <answer trueAnswer='false'>Boston</answer>
    </question>
    <question text='Combien de saison a durée la série X files ?'>
        <answer trueAnswer='false'>1</answer>
        <answer trueAnswer='false'>5</answer>
        <answer trueAnswer='true'>7</answer>
        <answer trueAnswer='false'>9</answer>
    </question> 
</questions>

In fact my XML is loading but this code :

NodeList nList = document.getElementsByTagName("question");
Toast.makeText(getBaseContext(),nList.getLength(), 
                    Toast.LENGTH_LONG).show();

Only work in Java not android.

Thank all for the answers

///////////////////////////////////////////////////////

Its working, with this code :

AssetManager assetManager = getAssets();
document =    builder.parse(assetManager.open("XML/questions.xml"));
NodeList nList = document.getElementsByTagName("question");

textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
                  textViewQuestion.setText(nList.item(0).getAttributes().getNamedItem("text").getNodeValue());
Was it helpful?

Solution

I have also face this problem create subdirectory in asset and put this file to subdirectory and givepath assetManager.open("subDir/questions.xml");

It will work..check it..

OTHER TIPS

Clean and build the app and then start your app again

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top