Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class Main {
    public static List<Map<String, Object>> xmlPullParser(String xmlString, String tagName, String[] tagNameList) {

        List<Map<String, Object>> list = null;
        Map<String, Object> map = null;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser pullParser = factory.newPullParser();

            InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

            pullParser.setInput(is, "utf-8");
            int event = pullParser.getEventType();

            while (event != XmlPullParser.END_DOCUMENT) {
                String nodeName = pullParser.getName();

                switch (event) {
                case 0:
                    list = new ArrayList<Map<String, Object>>();
                    break;
                case 2:
                    if (nodeName.equals(tagName)) {
                        map = new HashMap<String, Object>();
                    }

                    for (int i = 0; i < tagNameList.length; i++) {
                        if (nodeName.equals(tagNameList[i])) {
                            map.put(tagNameList[i], pullParser.nextText());
                        }
                    }
                    break;
                case 3:
                    if (tagName.equals(nodeName)) {
                        list.add(map);
                    }
                    break;
                }
                event = pullParser.next();
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}