0

我想解析一个基于 id 区分数据的 XML(可以说是表格形式)并在 Textviews 中显示它(一次一个 id)你能在这方面帮助我吗?最好在 XML PullParser 中,因为它是 android 推荐的

我希望 XML 值根据其 ID 一次放入 Textviews

<myXmlStarts>
<Id>1</id>
<EnglishData>"this is 1st English line"</EnglishData>
<UrduData>"this is 1st Urdu line"</UrduData>
</myXmlStarts>

<myXmlStarts>
<Id>2</id>
<EnglishData>"this is 2nd English line"</EnglishData>
<UrduData>"this is 2nd Urdu line"</UrduData>
</myXmlStarts>
4

1 回答 1

0

你可以做类似的事情,

public class TestClass {


    static String text;

       public static void main (String args[])
                 throws XmlPullParserException, IOException
             {
                 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                 factory.setNamespaceAware(true);
                 XmlPullParser xpp = factory.newPullParser();


                 List<Model> listData = new ArrayList<Model>();
                 Model model = null;
                 String xmlString = "<myXmlStarts><Id>1</Id><EnglishData>this is 1st English line</EnglishData><UrduData>this is 1st Urdu line</UrduData></myXmlStarts><myXmlStarts><Id>2</Id><EnglishData>this is 2nd English line</EnglishData><UrduData>this is 2nd Urdu line</UrduData></myXmlStarts>";
                 xpp.setInput(new StringReader (xmlString));
                 int eventType = xpp.getEventType();
                 while (eventType != XmlPullParser.END_DOCUMENT) {

                     String tagname = xpp.getName();

                     switch (eventType) {
                        case XmlPullParser.START_TAG:

                              if (tagname.equalsIgnoreCase("myXmlStarts")) {
                                   System.out.println("<myXmlStarts>");

                                   model = new Model();
                                }                         

                            break;

                        case XmlPullParser.TEXT:
                            text = xpp.getText();
                            break;

                        case XmlPullParser.END_TAG:

                            if (tagname.equalsIgnoreCase("myXmlStarts")) {
                                   System.out.println("</myXmlStarts>");
                                 listData.add(model);
                            } 

                            if (tagname.equalsIgnoreCase("Id")) {
                                   System.out.println("Id is : " +text);
                                   model.setId(text);
                                } 

                            if (tagname.equalsIgnoreCase("EnglishData")) {
                                   System.out.println("english data is : " +text);
                                   model.setEngData(text);
                                } 

                            if (tagname.equalsIgnoreCase("UrduData")) {
                                   System.out.println("urdudata is : " +text);
                                   model.setUrduData(text);
                            } 


                            // ....add what are the tags you want


                        default:
                            break;

                    }

                  eventType = xpp.next();
                 } // end of while loop


                 System.out.println("Array list size : " +listData.size());
                 int listSize = listData.size();


                 for (int i = 0; i < listSize; i++) {

                     System.out.println("Id : " + listData.get(i).getId());
                     System.out.println("Id : " + listData.get(i).getEngData());
                     System.out.println("Id : " + listData.get(i).getUrduData());

                }

             }

}

模型类

public class Model {

    String id, engData, urduData;

    public String getEngData() {
        return engData;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setEngData(String engData) {
        this.engData = engData;
    }
    public String getUrduData() {
        return urduData;
    }
    public void setUrduData(String urduData) {
        this.urduData = urduData;
    }

}

输出 :

<myXmlStarts>
Id is : 1
english data is : this is 1st English line
urdudata is : this is 1st Urdu line
</myXmlStarts>
<myXmlStarts>
Id is : 2
english data is : this is 2nd English line
urdudata is : this is 2nd Urdu line
</myXmlStarts>
Array list size : 2
Id : 1
Id : this is 1st English line
Id : this is 1st Urdu line
Id : 2
Id : this is 2nd English line
Id : this is 2nd Urdu line

我所做的是,

  1. 为 Id、English Data、UruduData 创建了一个模型。
  2. ArraList将带有参数声明为Model.
  3. 每当它看到myXmlStartsSTART_TAG 时,它只是初始化模型类。(模型=新模型();)
  4. 一旦找到 END_TAG等Id,您只需添加到模型中EnglishData
  5. 但是,如果它发现 END_TAG 为myXmlStarts,它只是将它添加到ArrayList(listData.add(model);) 并且同样的事情会发生,直到文档结束。

现在,您拥有ArrayList. 只需将这些值添加到textView. 我刚刚完成了你的一半工作。休息,你必须尝试。祝你好运。

笔记 :

您的 xml 标签中有错误。

改变

<Id>1</id>

<Id>1</Id> <-- closing and opening tag name should be same. Not `i` once and 'I' later. Good luck.
于 2015-04-11T19:02:44.583 回答