Local XML parsing

Brencezep

New Member
I am developing a quiz app in which I had to parse an XML. This is done and I have got the output which I wanted. Now, I have to parse the same XML file and it will be stored locally in my application. How do I store the file locally and then parse it?Here is the activity class:\[code\]public class QuizActivity extends Activity { //strings for use String answer_str, option1_str,option2_str,option3_str,option4_str; //text views for quiz layout TextView question_view; TextView option1; TextView option2; TextView option3; TextView option4; int counter=0; int loop_checker=0; int i; // All static variables static final String URL = "http://gujaratimandal.org/data.xml"; // XML node keys static final String KEY_MCHOICE = "mchoice"; // parent node static final String KEY_QUESTION = "question"; static final String KEY_ID = "id"; static final String KEY_OPTION1 = "option1"; static final String KEY_OPTION2 = "option2"; static final String KEY_OPTION3 = "option3"; static final String KEY_OPTION4 = "option4"; static final String KEY_ANSWER = "answer"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); //assigning objects to layouts question_view=(TextView)findViewById(R.id.question_textView); option1=(TextView)findViewById(R.id.option1_textView); option2=(TextView)findViewById(R.id.option2_textView); option3=(TextView)findViewById(R.id.option3_textView); option4=(TextView)findViewById(R.id.option4_textView); //calling function to populate ui populating_textview(counter); //onclick opt 1 option1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //getting text from TextView and checking whether it is equal to ans option1_str=option1.getText().toString(); if (answer_str.equalsIgnoreCase(option1_str)) { makeAToast("Correct!"); //calling function to populate ui with next question counter++; if (counter<=loop_checker) { populating_textview(counter); } else { makeAToast("Game Over!"); } } else { makeAToast("Wrong answer!"); } } }); //onclick opt 2 option2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //getting text from TextView and checking whether it is equal to ans option2_str=option2.getText().toString(); String answer=answer_str; if (answer.equalsIgnoreCase(option2_str)) { //calling function to populate ui with next question makeAToast("Correct!"); counter++; if (counter<=loop_checker) { populating_textview(counter); } else { makeAToast("Game Over!"); } } else { makeAToast("Wrong answer!"); } } }); //onclick opt 3 option3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //getting text from TextView and checking whether it is equal to ans option3_str=option3.getText().toString(); String answer1=answer_str; if (answer1.equalsIgnoreCase(option3_str)) { //calling function to populate ui with next question makeAToast("Correct!"); counter++; if (counter<=loop_checker) { populating_textview(counter); } else { makeAToast("Game Over!"); } } else { makeAToast("Wrong answer!"); } } }); //onclick opt 4 option4.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //getting text from TextView and checking whether it is equal to ans option4_str=option4.getText().toString(); String answer2=answer_str; if (answer2.equalsIgnoreCase(option4_str)) { //calling function to populate ui with next question makeAToast("Correct!"); counter++; if (counter<=loop_checker) { populating_textview(counter); } else { makeAToast("Game Over!"); } } else { makeAToast("Wrong answer!"); } } }); } // function to populate ui with question counter void populating_textview(int count_questions) { ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element //count_questions=2; NodeList nl = doc.getElementsByTagName(KEY_MCHOICE); // looping through all item nodes <item> for ( i = 0; i < nl.getLength();i++) { loop_checker=i;// while(counter< nl.getLength())// { // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); Element e = (Element) nl.item(count_questions); // adding each child node to HashMap key => value map.put(KEY_MCHOICE, parser.getValue(e, KEY_MCHOICE)); map.put(KEY_ID, parser.getValue(e, KEY_ID)); map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION)); question_view.setText(parser.getValue(e, KEY_QUESTION)); map.put(KEY_OPTION1, parser.getValue(e, KEY_OPTION1)); //option1_str =parser.getValue(e, KEY_OPTION1); option1.setText(parser.getValue(e, KEY_OPTION1)); map.put(KEY_OPTION2, parser.getValue(e, KEY_OPTION2)); option2.setText(parser.getValue(e, KEY_OPTION2)); //option2_str =parser.getValue(e, KEY_OPTION2); map.put(KEY_OPTION3, parser.getValue(e, KEY_OPTION3)); option3.setText(parser.getValue(e, KEY_OPTION3)); //option3_str =parser.getValue(e, KEY_OPTION3); map.put(KEY_OPTION4, parser.getValue(e, KEY_OPTION4)); option4.setText(parser.getValue(e, KEY_OPTION4)); //option4_str =parser.getValue(e, KEY_OPTION4); map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER));// makeAToast(parser.getValue(e, KEY_ANSWER)); answer_str =parser.getValue(e, KEY_ANSWER); // adding HashList to ArrayList menuItems.add(map); } } public void makeAToast(String str) { Toast toast = Toast.makeText(this,str, Toast.LENGTH_LONG); toast.setGravity(Gravity.BOTTOM, 0, 0); toast.setDuration(1000000); toast.show(); }}\[/code\]This class is used to parse XML from URL:\[code\]public class XMLParser { // constructor public XMLParser() { } /** * Getting XML from URL making HTTP request * @param url string * */ public String getXmlFromUrl(String url) { String xml = null; try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); xml = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // return XML return xml; } /** * Getting XML DOM element * @param XML string * */ public Document getDomElement(String xml){ Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage()); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage()); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage()); return null; } return doc; } /** Getting node value * @param elem element */ public final String getElementValue( Node elem ) { Node child; if( elem != null){ if (elem.hasChildNodes()){ for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){ if( child.getNodeType() == Node.TEXT_NODE ){ return child.getNodeValue(); } } } } return ""; } /** * Getting node value * @param Element node * @param key string * */ public String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); }}\[/code\]How do I modify the classes to parse a local XML file? Where should I include the XML file?
 
Back
Top