我不确定它是否是一个明确的问题。我想要的是从 web 服务中检索 xml 响应。我有 web 服务的 url、用户名、密码、xml 正文等详细信息。我可以在字符串中获取 xml 响应变量。有人可以为我提供一个有用的链接来解析 xml 字符串吗?我共享用于检索 xml 的代码注意:-确保您有 commons-httpclient-3.1、commons-codec-1.6、commons-logging-1.1.1、junit-4.10 库
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 接口
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";
}
菜单服务接口
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 );
}
}
然后在一些活动中写
MenuService menuService = new MenuServiceImpl();
String prMenu = menuService.getMenu();
Assert.assertNotNull( prMenu );
test.setText(prMenu);
现在我将 xml 响应存储在 prMenu 变量中。它看起来像这样 http://www.coders-global.com/works/dev/menuserivicetemp.xml。现在我该如何解析这个 Xml 字符串。请采取看了一下链接。它看起来很复杂,我之前在其他一些线程中询问过如何解析这个链接,并且回复没有那么有用。如果有任何有用的链接或建议,请告诉。