在android中解析具有多个子节点的简单XML

时间:2014-07-24 10:22:39

标签: android xml parsing

我有一个包含多个子节点的XML文件。

让我解释一下,我的文件看起来像这样:

<Category id="1Category">
 <Category id="2Category">
  <Name>second category</Name>
   <Category id="3Category">
    <Name>Third category</Name>
   </Category>
 </Category>
</Category>

问题:我不知道子类的数量,它可以是1或更多。 如何在不知道其编号的情况下阅读以下类别?

这是我的序列化器:

            Strategy strategy = new TreeStrategy("Structure",
                        "CategorySchemes");
                Serializer serializerCategory = new Persister(strategy);
                Structure structure = serializerCategory.read(
                        Structure.class, reader, false);
                Schedario.listStructure.clear();
                Schedario.listStructure.add(structure);
                Intent iCategory = new Intent(context,
                        CategorySchemeActivity.class);
                context.startActivity(iCategory);
                break;

2 个答案:

答案 0 :(得分:1)

这是我的示例xml,代码可能适合你。

 <lesson>

    <id>1</id>
    <title>Kamet</title>
    <video>/Videos/elif-ba/2.mp4</video>

    <chars>
        <item>
            <char>1</char>
            <meaning>Anlam 3</meaning>
            <sound>sound_ba/EbDers1/Sounds/</sound>
        </item>
        <item>
            <char>2</char>
            <meaning>Anlam 3_1</meaning>
            <sound>sound_ba/EbDers1/Sounds/</sound>
        </item>
        <item>
            <char>3</char>
            <meaning>Anlam 2_2</meaning>
            <sound>sound_ba/EbDers1/Sounds/</sound>
        </item>
        <item>
            <char>4</char>
            <meaning>Anlam 3_3</meaning>
            <sound>sound_ba/EbDers1/Sounds/</sound>
        </item>
    </chars>

</lesson>

解析器方法;

public List<ModelEzan> parseEzanKametData() {

    ezanModel = new ArrayList<ModelEzan>();
    try {

        XmlPullParser xpp = CustomXmlPullParserFactory.create(assetManager.open("xml/data_ezan.xml"));

        String currentTag = "";

        while (xpp.next() != XmlPullParser.END_DOCUMENT) {

            if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("lesson")) {
                currentSubEzan = new ModelEzan();
                Log.w("lessonTag", "Start");
            }

            if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("item")) {
                model = new ItemModel();
                Log.w("charsTag", "Start");
            }

            if (xpp.getEventType() == XmlPullParser.END_TAG && xpp.getName().equals("lesson")) {
                ezanModel.add(currentSubEzan);
                Log.w("lessonTag", "End");
            }

            if (xpp.getEventType() == XmlPullParser.END_TAG && xpp.getName().equals("item")) {
                currentSubEzan.setModelItems(model);
                Log.w("lessonTag", "End");
            }

            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                currentTag = xpp.getName();
            }

            if (xpp.getText() == null || xpp.getText().trim().equals("")) {
                continue;
            }

            if (xpp.getEventType() == XmlPullParser.TEXT) {

                if (currentTag.equalsIgnoreCase("title")) {
                    currentSubEzan.title = xpp.getText().trim();
                    Log.w("title", xpp.getText().trim());
                }

                if (currentTag.equalsIgnoreCase("video")) {
                    currentSubEzan.video = xpp.getText().trim();
                    Log.w("video", xpp.getText().trim());
                }

                if (currentTag.equalsIgnoreCase("char")) {
                    model.character = xpp.getText().trim();
                    Log.w("char", xpp.getText().trim());
                }

                if (currentTag.equalsIgnoreCase("meaning")) {
                    model.meaning = xpp.getText().trim();
                    Log.w("meaning", xpp.getText().trim());
                }

                if (currentTag.equalsIgnoreCase("sound")) {
                    model.sound = xpp.getText().trim();
                    Log.w("sound", xpp.getText().trim());
                }

            }
        }
    } catch (Exception e) {
        return null;
    } finally {

        return ezanModel;
    }
}

答案 1 :(得分:0)

我建议创建一个使用递归调用的方法。

示例:

    private class Category {
    Category cat;

    // get; set;
}

// On your parser

public void populateCategoryObj(Category cat){
    //Pull is an example.
    YourXmlPullParser xpp = new YourXmlPullParser()
    int eventType = xpp.next();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            currentTag = xpp.getName();
            if (currentTag.equals("Category")) {
                Cat newCat = new Category();
                cat.setCategory(populateCategoryObj(newCat));
            }
        }

        else if(eventType == XmlPullParser.TEXT){
            // Get the name tag text and populate your Category obj
        }

        eventType = xpp.next();
    }
}