How to parse xml stored in a string variable? Where do we get the xml response from a web service that has authentication?

I'm not sure if he has a clear question or not. What I want is to get an xml response from webservice. I have url, username, password, xml body, etc. Details webservice. And I could get the xml response in a string variable. Can someone provide me with a useful link for parsing an xml string? I use code to extract xml Note: -Make sure you have commons-httpclient-3.1, commons-codec-1.6, commons-logging-1.1.1, junit-4.10 library

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 will write in some activity

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

Now I have an xml answer with me, which is stored in the prMenu variable. And it will look like this http://www.coders-global.com/works/dev/menuserivicetemp.xml.Now How can I parse this Xml line. Please take a look at the link. It looks complicated, and I asked how to parse this link earlier in some other topic, and the answers were not helpful. If anyone has helpful links or suggestions, please let me know.

+4
source share
1 answer

Your question seems to be equal to β€œHow to parse XML content stored in memory”, since you seem to be able to correctly capture data from a remote server.

In fact, there are two tools for Java built-in libraries called SAX and DOM parsers, respectively. These two options work in a completely different way, and it is important to understand the differences and choose between them wisely.

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

PS: also using String.append, since you are doing pretty poorly in terms of performance - you need to look at the stringbuilder classes optimized for this kind of task.

0
source

Source: https://habr.com/ru/post/1432072/


All Articles