我想從我的res
文件夾傳遞一個.xml
文件到XMLReader
以便將其解析到一個對象:傳遞XML資源的XMLReader
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);
}
}
我相信,這是該行:
inStream.setCharacterStream(new StringReader(getResources().getXml(R.xml.questions).toString()));
有人能指導我這個.xml
文件傳遞給XMLReader
。
編輯:
將其改爲:
InputSource inStream = new InputSource(getResources().openRawResource(R.raw.questions));
感謝但是當我使用你的修改時,我得到的幫助:XMLReader類型中的方法解析(InputSource)不適用於參數(InputStream) – EHarpham