title: How to parse an xml stored in a string variable ?where we get xml response from a webservice which has authentication?
body: <p>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</p>
  
  <pre><code> 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;
          }
      }
  </code></pre>
  
  <p>WebServiceClientConstants interface</p>
  
  <pre><code>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  = "&lt;?xml version=\"1.0\"?&gt;";
      String  CONTENT_TYPE    = "Content-Type";
      String  APPLICATION_XML = "application/xml";
      String  UTF_8_ENCODING  = "UTF-8";
  }
  </code></pre>
  
  <p>MenuService interface</p>
  
  <pre><code>public interface MenuService
  {
      String getMenu();
  }
  </code></pre>
  
  <p>MenuServiceImpl.java</p>
  
  <pre><code>public class MenuServiceImpl extends AbstractService implements MenuService
  {
      @Override
      public String getMenu()
      {
          String requestUrl = "getMenu";
          String requestBody = "&lt;ServiceRequest&gt;" + "&lt;ShortName&gt;AppName&lt;/ShortName&gt;"
                  + "&lt;/ServiceRequest&gt;";
  
          return postForString( requestUrl, requestBody );
      }
  }
  </code></pre>
  
  <p>Then a in some activity write</p>
  
  <pre><code> MenuService menuService = new MenuServiceImpl();
          String prMenu = menuService.getMenu();
          Assert.assertNotNull( prMenu );
  
          test.setText(prMenu);
  </code></pre>
  
  <p>Now i have the xml response with me  stored in prMenu variable.And it will look like this
  <a href="http://www.coders-global.com/works/dev/menuserivicetemp.xml.Now" rel="nofollow">http://www.coders-global.com/works/dev/menuserivicetemp.xml.Now</a> 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.</p>