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.
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.