Passing XML resource to XMLReader.

feliponI

New Member
I am trying to pass a .xml file from my res folder to an XMLReader in order to parse it to an object:\[code\] private void parseXML() { String parsedDatahttp://stackoverflow.com/questions/10996136/= ""; 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 = http://stackoverflow.com/questions/10996136/parsedData +"----->\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"Queston No: " + question.getQuestionNo() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"QuestionText: " + question.getQuestionText() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"CorrectAnswer: " + question.getQuestionText() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"WrongAnswer1: " + question.getQuestionText() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"WrongAnswer2: " + question.getQuestionText() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"WrongAnswer3: " + question.getQuestionText() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"Answered: " + question.isAnswered() + "\n"; parsedData = http://stackoverflow.com/questions/10996136/parsedData +"QuestionText: " + question.isAnsweredCorrectly() + "\n"; } Log.w("AndroidParseXMLActivity", "Done"); } catch (Exception e) { Log.w("AndroidParseXMLActivity",e ); } xmlOutput.setText(parsedData); }\[/code\]I am sure that it is the line:\[code\]inStream.setCharacterStream(new StringReader(getResources().getXml(R.xml.questions).toString()));\[/code\]Could someone guide me to pass this .xml file to the XML reader.edit:changed it to\[code\]InputSource inStream = new InputSource(getResources().openRawResource(R.raw.questions));\[/code\]
 
Back
Top