how to parse xml data from HTTPResponse in android

643 views Asked by At

i have made an activity in android,in that i have made a multipart entity request using HttpPost,Now i am getting successfull respose also.but thing is i dont know how to get those data from response.i have tried number of links for parsing xml but with no luck.Please help me for this.how to get data from my xml respose.My code is as below:

login

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Consts.API_HOST + "/login");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            nameValuePairs.add(new BasicNameValuePair("apiKey",
                    "JU7Jqt6X"));
            nameValuePairs.add(new BasicNameValuePair("type", "xml"));
            nameValuePairs.add(new BasicNameValuePair("email",
                    "[email protected]"));
            nameValuePairs.add(new BasicNameValuePair("pwd", "123"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // xml response..!jigar...
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost,
                    responseHandler);


            // end of res jigar...
            System.out
                    .println("::::::::::::::::::::::::;;MY RESPONSE IN LOGIN ATIVITY::::::::::"
                            + responseBody);

            // making doc
            Document doc = null;
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader sr = new StringReader(responseBody);
            InputSource is = new InputSource(sr);
            doc = builder.parse(is);



        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } catch (Exception e) {

        System.out
                .println("::::::::::::::::::::::::::::MY exception in edit::::::::::::::::"
                        + e.getMessage());

        return null;
    }
    return null;

    // Parsing Procedure......

Response

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<id>
8
</id>
<personal_title>
Mr.
</personal_title>
<first_name>
a
</first_name>
<middle_name>
b
</middle_name>
<last_name>
c
</last_name>
<email>
[email protected]
</email>
<password>
202cb962ac59075b964b07152d234b70
</password>
<mobile_number>
1234567890
</mobile_number>
<p_first_name>

</p_first_name>
<p_last_name>

</p_last_name>
<p_card_type>

</p_card_type>
<p_card_number>

</p_card_number>
<p_sec_code>

</p_sec_code>
<p_exp_month>

</p_exp_month>
<p_exp_year>

</p_exp_year>
<user_activation_key>
14164668001
</user_activation_key>
<varification>
1
</varification>
<send_mail>
0
</send_mail>
<status>
0
</status>
<register_date>
2014-11-20 23:04:14
</register_date>
<last_visit_date>
2014-11-20 23:04:14
</last_visit_date>
</root>
2

There are 2 answers

2
jigar On BEST ANSWER

I got my answer by my way.Hello all for your responses but i got myy question solved by my way ,Actually all are telling me about different parsing but i want to know about the parsing from HTTP RESPONSE,So i have got my solution,here it is: http://www.java2s.com/Code/Android/Development/getCharacterDataFromElement.htm

Thanks for this useful post.

0
Shaktisinh Jadeja On
            String xml="<yourxml></yourxml>"

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/xml");

            StringEntity entity = new StringEntity(xml);
            httpPost.setEntity(entity);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);