how to parse xml data from HTTPResponse in android

你说的曾经没有我的故事 提交于 2020-01-06 12:52:11

问题


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",
                    "yogesh@amarinfotech.com"));
            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>
yogesh@amarinfotech.com
</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>

回答1:


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.




回答2:


            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);


来源:https://stackoverflow.com/questions/27059729/how-to-parse-xml-data-from-httpresponse-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!