Android XML to JSON Convert XMLtoJson(String content, String method, String charest)

Here you can find the source of XMLtoJson(String content, String method, String charest)

Description

XM Lto Json

Declaration

public static String XMLtoJson(String content, String method,
            String charest) 

Method Source Code

//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;

public class Main {
    public static String XMLtoJson(String content, String method,
            String charest) {//from   www.  ja  v a 2 s  .  c o  m
        boolean isHas = false;
        String result = "";
        try {
            content = content.trim();
            ByteArrayInputStream bis = new ByteArrayInputStream(
                    content.getBytes());
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(bis, charest);
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if (isHas) {
                        result = parser.nextText();
                        return result;
                    }
                    if ((method + "Response").equals(parser.getName())) {
                        isHas = true;
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
                }
                event = parser.next();
            }// end while
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}