2012-12-03 52 views
2

我在製作測驗應用程序。對於MCQ的問題,我有一個我已經解析過的XML。 這是XML解析XML並在TextView中顯示

<quiz> 
<mchoice> 
<question>Not a team sport for sure</question> 
<id>1</id> 
<option1>Cricket</option1> 
<option2>Tennis</option2> 
<option3>Rugby</option3> 
<option4>Soccer</option4> 
<answer>Tennis</answer> 
</mchoice> 
<mchoice> 
<question>I am the biggest planet in the Solar System</question> 
<id>2</id> 
<option1>Saturn</option1> 
<option2>Jupiter</option2> 
<option3>Neptune</option3> 
<option4>Pluto</option4> 
<answer>Jupiter</answer> 
</mchoice> 
<mchoice> 
<question>I am the closest star to the earth</question> 
<id>3</id> 
<option1>Milky way</option1> 
<option2>Moon</option2> 
<option3>Sun</option3> 
<option4>North Star</option4> 
<answer>Sun</answer> 
</mchoice> 
<mchoice> 
<question>A number which is not prime</question> 
<id>4</id> 
<option1>31</option1> 
<option2>61</option2> 
<option3>71</option3> 
<option4>91</option4> 
<answer>91</answer> 
</mchoice> 
<mchoice> 
<question>Which is correct?</question> 
<id>5</id> 
<option1>Foreine</option1> 
<option2>Fariegn</option2> 
<option3>Foreig</option3> 
<option4>Foreign</option4> 
<answer>Foreign</answer> 
</mchoice> 
</quiz> 

這是XML解析器類我所用:

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)); 
     } 
} 

這是從我打電話XML解析器類和活動將解析的數據添加到其中包含hashMap的Arraylist中

// 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.main); 



     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 

     NodeList nl = doc.getElementsByTagName(KEY_MCHOICE); 
     // looping through all item nodes <item> 
     for (int i = 0; i < nl.getLength(); i++) { 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      Element e = (Element) nl.item(i); 
      // 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)); 
      //map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION)); 

      map.put(KEY_OPTION1, parser.getValue(e, KEY_OPTION1)); 
      map.put(KEY_OPTION2, parser.getValue(e, KEY_OPTION2)); 
      map.put(KEY_OPTION3, parser.getValue(e, KEY_OPTION3)); 
      map.put(KEY_OPTION4, parser.getValue(e, KEY_OPTION4)); 
      map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER)); 
      // adding HashList to ArrayList 
      menuItems.add(map); 


      for (HashMap.Entry<String, String> entry : map.entrySet()) { 
       String display_id=entry.getKey(); 
       String display_question = entry.getValue(); 
       makeAToast(""+display_id+":"+ display_question); 

      } 


     } 

     makeAToast(""+menuItems.size()); 
} 

    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(); 
     } 

問題是,數據正在檢索,但不是以所需的方式。

我要在以下格式檢索數據:

Format Required

,這樣我可以從每一個問題填充這些TextViews與數據:

Require to populate

我該怎麼辦?

+0

我不明白你的問題:您正在使用的祝酒詞,但你不希望有祝酒詞? – njzk2

+1

您是否收到錯誤?因爲您已經解析了XML文件,並且唯一要做的就是將這些值放入地圖列表中,然後將每個選項分配給一個變量,然後將值插入到屏幕上的文本視圖中。 – kabuto178

+0

安排你的東西佈局。你得到的價值使用小工具以正確的方式。 –

回答

4

使一個字符串中您公共類活動

字符串keyoption,keyoption2,名稱;

for (int i = 0; i < nl.getLength(); i++) { 

    HashMap<String, String> map = new HashMap<String, String>(); 
    Element e = (Element) nl.item(i); 

    keyoption = parser.getValue(e, KEY_OPTION1); 
    keyoption2 = parser.getValue(e, KEY_OPTION2); 

    map.put(KEY_OPTION1, keyoption); 
    map.put(KEY_OPTION2, keyoption2); 
} 

name.setText(keyoption); 
0

爲您提到的佈局創建一個xml文件。

它很簡單。

  1. 創建一個LinearLayout作爲xml的根。 (這是反正默認值)android:gravity="center"

  2. 看起來你需要5個textview在LinearLayout裏面。一個問題。 4爲您的選擇。所有人都用android:gravity="center_horizontal"

  3. 一個EditText到底爲答案。