Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringReader;
import java.util.HashMap;

import java.util.Stack;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class Main {
    public static boolean parseXMLMessage(String msg, HashMap<String, String> strMap) {
        boolean bRet = true;

        // parse xml document.
        XmlPullParserFactory factory;
        XmlPullParser parser;

        try {
            factory = XmlPullParserFactory.newInstance();
            parser = factory.newPullParser();
            Stack eleStack = new Stack();
            int parserEvent;

            parser.setInput(new StringReader(msg));
            parserEvent = parser.getEventType();

            while (parserEvent != XmlPullParser.END_DOCUMENT) {
                switch (parserEvent) {
                case XmlPullParser.START_TAG:
                    String newtag = parser.getName();
                    if (newtag.compareTo("xml") != 0) {
                        eleStack.push(newtag);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().compareTo("xml") != 0) {
                        eleStack.pop();
                    }
                    break;
                case XmlPullParser.TEXT:
                    String tagkey = "";
                    for (int i = 0; i < eleStack.size(); i++) {
                        tagkey += eleStack.elementAt(i);
                        if (i < eleStack.size() - 1)
                            tagkey += "_";
                    }
                    strMap.put(tagkey, parser.getText());
                    break;
                default:
                    break;
                }

                parserEvent = parser.next();
            }

            eleStack = null;
            parser = null;
            factory = null;
        } catch (Exception e) {
            e.printStackTrace();
            bRet = false;
        }

        return bRet;
    }
}