2012-07-12 3 views
0

내가 내가 내가 가지고있는 이러한 선택을 클릭하면 ID와 parsing.Also 동안 값 모두 내가 3 가지 buttons.So에서 이러한 옵션을 설정 한 가야안드로이드에서 파싱하는 방법에 attibute 및 값을 가져 오는 방법은 무엇입니까?

<question_choices> 
<question id="499">What do you call a chicken with bad sunburn</question> 
<choices1 id="2231">Burned Chicken</choices1> 
<choices2 id="2230">Fried Chicken</choices2> 
<choices3 id="2232">Dead Chicken</choices3> 
<choices_answer>Fried Chicken</choices_answer> 
</question_choices> 

의 포맷을 갖는 XML 파일을 값을 얻고 해당 ID도 저장해야합니다. getAttributes()를 사용하려고했으나 어디서 어떻게 사용하는지 알지 못합니다. 따라서 성공하지 못했습니다.

final XMLParser parser = new XMLParser(); 
    String xml = parser.getXmlFromUrl(URL); // getting XML 
    Document doc = parser.getDomElement(xml); // getting DOM element 
    final NodeList nl = doc.getElementsByTagName(KEY_QUESTION);   
     // looping through all item nodes <item> 
     for(int j=0;j<nl.getLength();j++) 
     { 
      Element e = (Element) nl.item(j); 
      listnew[j]=parser.getValue(e,KEY_QUEST); 
      options1[j]= parser.getValue(e, KEY_CHOICE1); 
         options2[j]= parser.getValue(e, KEY_CHOICE2); 
         options3[j]= parser.getValue(e, KEY_CHOICE3); 
     } 
     TextView question = (TextView)findViewById(R.id.question); 
     question.setText(listnew[x]); 

     opt1 = (Button)findViewById(R.id.opt1); 
     opt1.setText(options1[x]); 
     opt1.setOnClickListener(myOptionOnClickListener); 

     opt2 = (Button)findViewById(R.id.opt2); 
     opt2.setText(options2[x]); 
     opt2.setOnClickListener(myOptionOnClickListener); 

     opt3 = (Button)findViewById(R.id.opt3); 
     opt3.setText(options3[x]); 
     opt3.setOnClickListener(myOptionOnClickListener); 

     x++; 
    } 

구문 분석에 DOM 구문 분석기를 사용하고 있습니다. 클릭 한 내용을 저장해야합니다. 어떻게해야합니까?

+0

이이 http://stackoverflow.com/questions/4138754/getting-an-attribute-value-in-xml-element –

+1

(HTTP [이보기]를 참조하십시오 : // samir-mangroliya.blogspot.in/p/android-xml-parsing-using-dom-parser.html) –

+0

xml에서 값을 가져오고 있습니다. 499와 같은 ID를 가져와야합니다. – neethu

답변

0

이 시도 :

Document doc = parser.getDomElement(xml); 
String idQuestion = doc.getElementsByTagName("question").item(0) 
         .getAttributes().getNamedItem("id").getNodeValue(); 
String idChoise1 = doc.getElementsByTagName("choices1 ").item(0) 
         .getAttributes().getNamedItem("id").getNodeValue(); 
... 
+0

감사합니다 .. . worked ... – neethu

+0

나는 15 평판이 없기 때문에 나는 투표 할 수 없다. 죄송합니다. – neethu

+0

아니요, 대답은 평판이 필요하지 않습니다. 왼쪽에서 내 대답에 동의하십시오. http : // meta. stackexchange.com/a/5235 – R4j

관련 문제