如何在Android应用程序解析XML文件 [英] How to parse an XML file in an Android app

查看:140
本文介绍了如何在Android应用程序解析XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析XML文件,如下

I am trying to parse an XML file as below

<Subject>
    <chapter>
       <Question>abc</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
        <Question>def</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
         <Question>ccsv</Question>
        <answer>avksn</answer>
    </chapter>
</Subject>

在此,我能够指望章的数量。章的数量等于问题和答案的数目。我也有一个放置命名为OK按钮,在我的布局。

in this i am able to count the number of chapter. the number of chapter is equal to number of question and answer. i have also placed a button named as ok in my layout.

现在我要显示的第一个问题,然后单击确定后,我想要显示的第二个问题,它会走到最后。当我到达最后一个问题,我想搬到一个新的活动。

now i want to display the first question and after clicking ok i want to display the second question and it goes till the end. When i reached the last question i want to move to a new activity.

如何执行此,请帮助我

推荐答案

读取XML到InputStream和则:

Read the xml into a InputStream and then:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse([the InpuTstream]);

然后你可以使用DOC,如:

Then you can use doc like:

  if(doc.getElementsByTagName("GeometryCollection").getLength()>0){
    org.w3c.dom.Node parent_node = doc.getElementsByTagName("GeometryCollection").item(0);
    NodeList nl = parent_node.getChildNodes();
    for(int i = 0;i<nl.getLength();i++){
     ...

这篇关于如何在Android应用程序解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆