3

I am trying to pass a .xml file from my res folder to an XMLReader in order to parse it to an object:

private void parseXML() {
    String parsedData = "";
       try {
           Log.w("AndroidParseXMLActivity", "Start");
           /** Handling XML */
           SAXParserFactory spf = SAXParserFactory.newInstance();
           SAXParser sp = spf.newSAXParser();
           XMLReader xr = sp.getXMLReader();

           QuestionXMLHandler myXMLHandler = new QuestionXMLHandler();
           xr.setContentHandler(myXMLHandler);
           InputSource inStream = new InputSource();
           Log.w("AndroidParseXMLActivity", "Parse1");

           inStream.setCharacterStream(new StringReader(getResources().getXml(R.xml.questions).toString()));
           Log.w("AndroidParseXMLActivity", "Parse2");

           xr.parse(inStream);
           Log.w("AndroidParseXMLActivity", "Parse3");

           ArrayList<QuestionMaster> questionList = myXMLHandler.getQuestionsList();
           for(int i=0;i<questionList.size();i++){
                QuestionMaster question = questionList.get(i);
                parsedData = parsedData + "----->\n";
                parsedData = parsedData + "Queston No: " + question.getQuestionNo() + "\n";
                parsedData = parsedData + "QuestionText: " + question.getQuestionText() + "\n";
                parsedData = parsedData + "CorrectAnswer: " + question.getQuestionText() + "\n";
                parsedData = parsedData + "WrongAnswer1: " + question.getQuestionText() + "\n";
                parsedData = parsedData + "WrongAnswer2: " + question.getQuestionText() + "\n";
                parsedData = parsedData + "WrongAnswer3: " + question.getQuestionText() + "\n";
                parsedData = parsedData + "Answered: " + question.isAnswered() + "\n";
                parsedData = parsedData + "QuestionText: " + question.isAnsweredCorrectly() + "\n";
            }
            Log.w("AndroidParseXMLActivity", "Done");
        }catch (Exception e) {
            Log.w("AndroidParseXMLActivity",e );
        }
        xmlOutput.setText(parsedData);
    }
}

I am sure that it is the line:

inStream.setCharacterStream(new StringReader(getResources().getXml(R.xml.questions).toString()));

Could someone guide me to pass this .xml file to the XMLReader.

edit:

Changed it to:

InputSource inStream = new InputSource(getResources().openRawResource(R.raw.questions));
Yurets
  • 3,999
  • 17
  • 54
  • 74
EHarpham
  • 602
  • 1
  • 17
  • 34

3 Answers3

4
InputStream inStream = getResources().openRawResource(R.raw.questions);
xr.parse(inStream);
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Thanks for the help however when i use your changes i get : The method parse(InputSource) in the type XMLReader is not applicable for the arguments (InputStream) – EHarpham Jun 12 '12 at 12:19
3

getXml() returns an implementation of the XmlPullParser, and so it is already set to be parsed using that engine. Change your existing code to use XmlPullParser instead of SAX.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I think you should put the file either in raw or asset folder to use SAX parser and then using context read that file from there

Android - Read file from Assets http://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36