0

I have some problem with parsing XML using xmlpullparser API for Android.

Problem: I have a lot of duplication in Android classes like Activity or some business class for xml parser but it's the same!

Required: some base class or util for xmlpullparser to make all parsing in the same class; I just need send to him the URL for XML or for the WebService and I get the values and some utils if there is.

This what I have in the classes:

       int count = 0;
    try {
        srv = new URL(
                "URL Web Service");
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        xpp = factory.newPullParser();
        xpp.setInput(XMLUtility.getInputStream(srv), "UTF_8");
        insideItem = false;
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equalsIgnoreCase(tag)) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase(SId)) {
                    if (insideItem)
                        id.add(xpp.nextText()); // extract the headline
                } else if (xpp.getName().equalsIgnoreCase(SEn)) {
                    if (insideItem)
                        enable.add(xpp.nextText());
                }
            } else if (eventType == XmlPullParser.END_TAG
                    && xpp.getName().equalsIgnoreCase(tag)) {
                insideItem = false;
                checkEnable.add(id.get(count) + "," + enable.get(count));
                count++;
            }
            eventType = xpp.next();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

This code is duplicated; just change the tag. Any ideas?

4

1 回答 1

0

使用 xstream

http://x-stream.github.io/download.html

您可以为 xml 定义响应类,并且只定义对象类或 pojo 类。

于 2013-06-27T17:07:14.010 回答