如何解析存储在字符串变量中的xml?其中,我们从具有身份验证的Web服务获得xml响应



我不确定这是否是一个明确的问题。我想要的是从Web服务中检索xml响应。我有这个web服务的url、用户名、密码、xml主体等详细信息。我可以在一个字符串变量中得到xml响应。有人能为我提供一个有用的链接来解析xml字符串吗?我正在共享检索xml的代码注意:-请确保commons-httpclient-3.1,commons-codec-1.6,commons-logging-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();
}

菜单服务Impl.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.Now如何解析此Xml字符串。请看一下链接。它看起来很复杂,我之前在其他线程中询问过如何解析这个链接,但得到的回复没有那么大帮助。如果有任何有用的链接或建议,请告诉。

您的问题似乎相当于"我如何解析存储在内存中的XML内容",因为您似乎能够正确地从远程服务器获取数据。

本质上,Java库中内置了两种工具,分别称为SAX和DOM解析器。这两种选择的效果截然不同,重要的是你要理解其中的差异,并在它们之间做出明智的选择。

这里有一个在android XML解析教程中使用DOM解析器的例子,在数据量较低的情况下,这可能是您想要采取的方向。

附言:从性能的角度来看,同样使用String.append是非常糟糕的——你需要看看为这类任务优化的字符串生成器类。

最新更新