Android Http Get handleXMLResponse(HttpResponse response)

Here you can find the source of handleXMLResponse(HttpResponse response)

Description

handle XML Response

Declaration

public static Map handleXMLResponse(HttpResponse response) 

Method Source Code

//package com.java2s;

import java.io.StringReader;

import java.util.HashMap;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;

import org.apache.http.util.EntityUtils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
    public static Map handleXMLResponse(HttpResponse response) {
        Map<String, String> oauthResponse = new HashMap<String, String>();
        try {// w ww .j a  va  2  s  .  c  o  m

            String xmlString = EntityUtils.toString(response.getEntity());
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlString));
            Document doc = db.parse(inStream);

            System.out
                    .println("********** XML Response Received **********");
            parseXMLDoc(null, doc, oauthResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(
                    "Exception occurred while parsing XML response");
        }
        return oauthResponse;
    }

    public static void parseXMLDoc(Element element, Document doc,
            Map<String, String> oauthResponse) {
        NodeList child = null;
        if (element == null) {
            child = doc.getChildNodes();

        } else {
            child = element.getChildNodes();
        }
        for (int j = 0; j < child.getLength(); j++) {
            if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                org.w3c.dom.Element childElement = (org.w3c.dom.Element) child
                        .item(j);
                if (childElement.hasChildNodes()) {
                    System.out.println(childElement.getTagName() + " : "
                            + childElement.getTextContent());
                    oauthResponse.put(childElement.getTagName(),
                            childElement.getTextContent());
                    parseXMLDoc(childElement, null, oauthResponse);
                }

            }
        }
    }
}

Related

  1. requestGet(String url)
  2. executeHttpGet(String url)
  3. executeHttpPost(String url, ArrayList postParameters)
  4. getHTTP(String... params)
  5. handleURLEncodedResponse(HttpResponse response)
  6. getStringFromConnection( HttpURLConnection connection)
  7. httpGetRequestParseParams( String paramString)
  8. formatHttpHeaders(Map headers)
  9. getStringResponseData(HttpResponse httpResponse)