繁体   English   中英

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

[英]Parsing simple XML with multiple child nodes in android

我有一个带有多个子节点的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;

这是我的示例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;
    }
}

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

例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM