0

我正在做一个项目,我想动态地制作一个应用程序。

我想使用定义了所有模板的 xml 文件来做到这一点,这样我就可以解析它们并将它们应用于模板。

但是我有一个关于 xmlPullParser 的问题,当有很多标签和属性时,解析代码似乎很困难,又大又丑。

有一个更好的方法吗?

这是我目前用作示例的 xml:

    <?xml version="1.0" encoding="UTF-8"?>
<app>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">01</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">Het Verhaal</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">02</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">De Bruul</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">03</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">Grote markt</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">04</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">De Sint-Romboutstoren</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">05</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">De Vismarkt</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">06</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">Hanswijk</title>
    </template>
    <template type="chapter">
        <background>#10c1c5</background>
        <number color="#ffffff">07</number>
        <maxnumber color="#ffffff">07</maxnumber>
        <title color="#ffffff">De Vest</title>
    </template>


<template type="route">
    <text>Ga 150 meter verder</text>
    <background>#003e79</background>
</template>
<template type="route">
    <text>Ga 550 meter verder</text>
    <background>#0000FF</background>
</template>
<template type="route">
    <text>Ga 850 meter verder</text>
    <background>#00FF00</background>
</template>
</app>

这是解析它,制作正确的对象并设置颜色和文本的丑陋代码:

public List<Fragment> parse() {
        // parse de xml plaats de objecten in de list en return de list

        try {

            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false); // use namespace ?
            xpp = factory.newPullParser();
            // xmluit file laden
            String file = "assets/test.xml";
            InputStream in = this.getClass().getClassLoader()
                    .getResourceAsStream(file);
            xpp.setInput(in, null);

            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = xpp.getName(); // tag naam
                String atType = xpp.getAttributeValue(null, "type"); // atrribuut
                                                                        // type
                String atColor = xpp.getAttributeValue(null, "color");

                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("template")) {
                        if (atType.equalsIgnoreCase("route")) {
                            // fragments.add(new RouteFragment()); //nieuw
                            // routefragment toevoegen aan de lijst
                            route = new RouteFragment();
                            typeObject = "route";
                        } else if (atType.equalsIgnoreCase("chapter")) {
                            // fragments.add(new chapterFragment()); //nieuw
                            // chapterfragment toevoegen aan de lijst
                            chapter = new ChapterFragment();
                            typeObject = "chapter";
                        }

                    } else if (tagname.equalsIgnoreCase("number")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setNumberTextcolor(atColor);
                        }
                    } else if (tagname.equalsIgnoreCase("maxnumber")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setMaxNumberColor(atColor);
                        }
                    } else if (tagname.equalsIgnoreCase("title")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setTitleColor(atColor);
                        }
                    }
                    break;

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

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("template")) {
                        if (typeObject.equalsIgnoreCase("route")) {
                            fragments.add(route); // nieuw routefragment
                                                    // toevoegen aan de lijst

                        }
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            fragments.add(chapter); // nieuw chapterfragment
                                                    // toevoegen aan de lijst
                        }
                    } else if (tagname.equalsIgnoreCase("text")) {
                        if (typeObject.equalsIgnoreCase("route")) {
                            route.setOmschrijving(text);
                        }
                    } else if (tagname.equalsIgnoreCase("background")) {
                        if (typeObject.equalsIgnoreCase("route")) {
                            route.setKleur(text);
                        } else if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setBackgroundColor(text);
                        }
                    } else if (tagname.equalsIgnoreCase("number")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setNumber(text);
                        }
                    } else if (tagname.equalsIgnoreCase("maxnumber")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setMaxNumber(text);
                        }
                    } else if (tagname.equalsIgnoreCase("title")) {
                        if (typeObject.equalsIgnoreCase("chapter")) {
                            chapter.setTitle(text);
                        }
                    }
                    break;
                default:
                    break;

                }
                eventType = xpp.next();
            }

        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return fragments;

    }
4

2 回答 2

0

您的代码中有太多嵌套if/else语句,因此难以阅读。您应该将代码分解为具体的方法,即parseApp()readTemplate()readBackground()readNumber()readMaxNumber()readTitle() 有关源代码的真实示例,请参阅 Google 的此页面:解析 XML 数据

于 2014-03-12T20:37:19.523 回答
0

我对你的建议是使用 boolean 它看起来更优雅和可读...下一个示例用于解析 New York Times 的 xml,从项目标签中获取标题内容我希望它在某种程度上有所帮助.. .

public class MainActivity extends Activity {
boolean item;
boolean title;
String titleContent;
String tagName;
String childrens="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources res = getResources();
    try
    {
        XmlPullParser parser = res.getXml(R.xml.wallarss);
        int eventType = parser.getEventType();
        while(eventType!=XmlPullParser.END_DOCUMENT)
        {
            if(eventType==XmlPullParser.START_TAG)
            {   
                tagName=parser.getName();

                if(item)
                {
                    if(tagName.equals("title"))
                    {
                        title=true;
                    }

                }
                else//!item
                {
                    if(tagName.equals("item"))
                        item=true;
                }
                if(eventType==XmlPullParser.END_TAG)
                {
                    tagName=parser.getName();
                    if(tagName.equals("item"))
                        item=false;
                }

            }
                if(eventType==XmlPullParser.TEXT)
                {
                    if(title)
                    {
                        String artical=parser.getText();
                        Log.i("news",artical);
                        title=false;
                    }
                    eventType=XmlPullParser.START_TAG;
                }


            eventType=parser.next();
        }
    }

    catch(XmlPullParserException ex)
    {
        Log.e("Xml reader problem", "canot read xml file", ex);
    }
    catch(IOException ex)
    {
        Log.e("I/O problem", "Input/Output Erorr", ex);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

于 2014-01-29T19:12:18.367 回答