Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中获取xml解析中的attibute和值?_Android_Xml Parsing - Fatal编程技术网

如何在android中获取xml解析中的attibute和值?

如何在android中获取xml解析中的attibute和值?,android,xml-parsing,Android,Xml Parsing,我有一个xml文件,格式为 <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> <choice

我有一个xml文件,格式为

<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>

晒伤严重的鸡叫什么
烧鸡
炸八块鸡
死鸡
炸八块鸡
我必须在解析过程中同时获取id和值。此外,我在3个不同的按钮中设置了这些选项。因此,当我单击这些选项时,我必须获取值,并且还必须保存相应的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++; 
     }
final XMLParser=new XMLParser();
字符串xml=parser.getXmlFromUrl(URL);//获取XML
Document doc=parser.getdoElement(xml);//获取DOM元素
最终节点列表nl=doc.getElementsByTagName(关键问题);
//循环通过所有项目节点
对于(int j=0;j试试这个:

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

看到这里,我正在获取xml中的值。我需要获取id,如499。不,接受答案不需要声誉,在我的答案中从左侧勾选接受,请阅读这里