Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:50:55+00:00 2026-06-10T17:50:55+00:00

Im not sure whether its a clear question or not.What i want is to

  • 0

Im not sure whether its a clear question or not.What i want is to retrieve xml response from a webservice.I have the url,username,password,xml body etc details of the webservice.And i could get the xml response in a string variable.Can some one provide me a useful link to parse an xml string? Im sharing the code for retrieving xml
Note:-Make sure you have
commons-httpclient-3.1,commons-codec-1.6,commons-logging-1.1.1,junit-4.10 libraries

 import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.PostMethod;


    public class AbstractService
    {
        @SuppressWarnings( "deprecation" )
        protected String postForString( final String requestUrl, final String requestBody )
        {
            final StringBuilder result = new StringBuilder();
            HttpClient client = new HttpClient();

            try
            {
                PostMethod postRequest = new PostMethod( getAbsoluteUrl( requestUrl ) );

                postRequest.addRequestHeader( WebServiceClientConstants.CONTENT_TYPE,
                        WebServiceClientConstants.APPLICATION_XML );
                postRequest.setRequestBody( WebServiceClientConstants.REQUEST_HEADER + requestBody );

                client.getState()
                        .setCredentials(
                                new AuthScope( WebServiceClientConstants.HOST, WebServiceClientConstants.PORT,
                                        AuthScope.ANY_REALM ),
                                new UsernamePasswordCredentials( WebServiceClientConstants.USERNAME,
                                        WebServiceClientConstants.PASSWORD ) );

                int responseCode = client.executeMethod( postRequest );

                System.out.println( "[REQUEST][" + postRequest.getURI().toString() + "]" );
                System.out.println( "[STATUS][" + postRequest.getStatusLine().toString() + "]" );

                if ( HttpStatus.SC_OK == responseCode )
                {
                    String data = null;
                    final InputStream responseStream = postRequest.getResponseBodyAsStream();
                    final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( responseStream,
                            WebServiceClientConstants.UTF_8_ENCODING ) );

                    while ( ( data = bufferedReader.readLine() ) != null )
                    {
                        result.append( data );
                    }
                }

                postRequest.releaseConnection();

            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }

            return result.toString();
        }

        private String getAbsoluteUrl( String requestUrl )
        {
            return WebServiceClientConstants.SERVIE_BASE_URL + requestUrl;
        }
    }

WebServiceClientConstants interface

package com.test.service.info;
public interface WebServiceClientConstants
{
    String  PROTOCOL        = "http://";
    String  HOST            = "youraddress.blah.test.com";
    Integer PORT            = 8080;
    String  SERVIE_BASE_URL = "http://youraddress.blah.test.com:8080/test/seam/resource/Services/";
    String  USERNAME        = "Username";
    String  PASSWORD        = "password";
    String  REQUEST_HEADER  = "<?xml version=\"1.0\"?>";
    String  CONTENT_TYPE    = "Content-Type";
    String  APPLICATION_XML = "application/xml";
    String  UTF_8_ENCODING  = "UTF-8";
}

MenuService interface

public interface MenuService
{
    String getMenu();
}

MenuServiceImpl.java

public class MenuServiceImpl extends AbstractService implements MenuService
{
    @Override
    public String getMenu()
    {
        String requestUrl = "getMenu";
        String requestBody = "<ServiceRequest>" + "<ShortName>AppName</ShortName>"
                + "</ServiceRequest>";

        return postForString( requestUrl, requestBody );
    }
}

Then a in some activity write

 MenuService menuService = new MenuServiceImpl();
        String prMenu = menuService.getMenu();
        Assert.assertNotNull( prMenu );

        test.setText(prMenu);

Now i have the xml response with me stored in prMenu variable.And it will look like this
http://www.coders-global.com/works/dev/menuserivicetemp.xml.Now how can i parse this Xml string.Please take a took look at the link.It looks complex and i had asked how to parse this link before in some other thread and the replies were not that helpful.If any have useful links or suggestions please tell.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:50:57+00:00Added an answer on June 10, 2026 at 5:50 pm

    Your question seems to amount to ‘How do I parse XML content stored in memory’ as you seem able to grab the data correctly from the remote server.

    Essentially there are two tools for this built into Java libraries called the SAX and DOM parsers respectively. These two options work quite differently and it is important that you understand the differences and choose intelligently between them.

    Here is an example of using the DOM parser in android XML Parsing Tutorial which is probably the direction you want to take given the low volume of the data.

    PS: also using String.append as you do is pretty bad from a performance point of view – you need to look at the stringbuilder classes that are optimised for this kind of task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure whether its a duplicate or not-why my left and right border are
I am not sure whether this only happens to me. Basically if I have
I'm not sure whether this question belongs on StackOverflow or SuperUser, but here goes
I am not sure whether distinct is the right word for this. I have
Not sure whether this is a MSMQ or NServiceBus question, but I'm wondering: Should
I'm not sure whether to post it here or at ServerFault. Anyway, I'm trying
I am not sure whether it has been disscussed before or not but I
I'm not sure whether or not this is the appropriate way of doing this,
I am not sure whether I am asking this with the right words, I
I'm not sure whether these are programming questions, but I'm sure lots of new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.