使用GWT读取非常大的本地XML文件 [英] Reading a very large local XML file using GWT

查看:117
本文介绍了使用GWT读取非常大的本地XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GWT构建我的第一个Java应用程序,它必须从非常大的XML文件读取数据。当我尝试发送文件中信息的请求时,我遇到了问题,而且我不太确定它是否与文件的大小或我的语义有关。在我的程序中,我有以下几点:

  static final String xmlurl =filename.xml; 
String xmlData;

...

public void onModuleLoad(){
requestData(xmlurl);
if(xmlData.equals(Error)){
//显示错误信息
return;
} else {
//显示xml
}

void requestData(String url){
final int STATUS_CODE = 200;

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,url);
尝试{
builder.setTimeoutMillis(2000);
builder.sendRequest(null,new RequestCallback(){
public void onError(Request request,Throwable exception){
xmlData =Error
}

public void onResponseReceived(Request request,Response response){$ b $ if(STATUS_CODE == response.getStatusCode()){
xmlData = response.getText();
} else {
xmlData =Error;
}
}
}
} catch(RequestException e){
xmlData =Error;
}
}

我重写了代码,所以我可能会犯一些错别字,但实际上应用程序编译和运行。问题是,当我尝试显示XML并且xmlData从不分配给任何东西时,我得到一个空指针异常。我试着放一个while循环,等待它存储Erroro r XML文本,但该变量从未分配给。我将XML文件保存在我的项目的war目录中,似乎能够找到该文件。我在网上搜索了类似的例子,但是一切似乎都比我想要做的更复杂一些,而且我不确定是否需要servlet或配置更改,或者文件是只是太大而无法读入字符串。任何帮助表示赞赏。感谢。

解决方案

在客户端解析xml(在浏览器中)很慢,应该避免;委托给服务器端的速度通常更快,因此用户更友好(大文件会导致浏览器停止响应很长时间)。

然而,这个决定是你的; )下面是我用来阅读文件的内容:



定义这个帮助器方法:

  public static void httpGetFile(final String url,final AsyncCallback< String> callback){
final RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,url);
rb.setCallback(new RequestCallback(){
public void onResponseReceived(Request request,Response response){
try {
final int responseCode = response.getStatusCode()/ 100;
if(url.startsWith(file:/)||(responseCode == 2)){
callback.onSuccess(response.getText());
} else {
callback.onFailure(new IllegalStateException(HttpError#+ response.getStatusCode()+ - + response.getStatusText()));
}
} catch(Throwable e){
callback.onFailure(e);
}
}

public void onError(Request request,Throwable exception){
callback.onFailure(exception);
}
});
尝试{
rb.send();
} catch(RequestException e){
callback.onFailure(e);




$ b $ p
$ b

在你的代码中,它可能看起来像这样:

  ... 
httpGetFile(url,new AsyncCallback< String>(){
public void onFailure Throwable caught){
xmlData =Error;
}

public void onSuccess(String xmlText){
xmlData = xmlText;
}
}
....


I am building my first Java application using GWT which must read in data from a very large XML file. I am having issues when I try sending a request for the information in the file, and I'm not quite sure if it has to do with the size of the file, or my semantics. In my program I have the following:

static final String xmlurl = "filename.xml";
String xmlData;

...

public void onModuleLoad() {
requestData(xmlurl);
if(xmlData.equals("Error")){
    // display error message
    return;
} else {
    // display the xml
}

void requestData(String url){
    final int STATUS_CODE = 200;

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
         builder.setTimeoutMillis(2000);
         builder.sendRequest(null, new RequestCallback() {
              public void onError(Request request, Throwable exception) {
                   xmlData = "Error" 
              }

              public void onResponseReceived(Request request, Response response)  {
                   if (STATUS_CODE == response.getStatusCode()){
                        xmlData = response.getText();
                   } else {
                        xmlData = "Error";
                   }
              }
         }
    } catch (RequestException e) {
         xmlData = "Error";
    }
}

I rewrote the code, so I might have made some typos, but for the actual application it compiles and runs. The issue is that I get a nullpointer exception when I try to display the XML and xmlData is never assigned to anything. I tried putting a while loop that waited for it to store either "Error" or the XML text, but the variable was never assigned to. I have the XML file saved in the war directory of my project, and it seems to be able to find the file. I've searched online for similar examples, but everything seemed to be a bit more complicated than what I'm trying to do, and I'm not sure if I need a servlet or a configuration change for this, or if the file is just too big to read into a String. Any help is appreciated. Thanks.

解决方案

Parsing xml at client-side (in browser) is quite slow, and should be avoided; delegating this to server-side is usually faster and therefore more user friendly (big files will cause your browser stop responding for a long time).

However the decision is yours ;) Here is what I use for reading files:

Define this helper method:

public static void httpGetFile(final String url, final AsyncCallback<String> callback) {
    final RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
    rb.setCallback(new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {
            try {
                final int responseCode = response.getStatusCode() / 100;
                if (url.startsWith("file:/") || (responseCode == 2)) {
                    callback.onSuccess(response.getText());
                } else {
                    callback.onFailure(new IllegalStateException("HttpError#" + response.getStatusCode() + " - " + response.getStatusText()));
                }
            } catch (Throwable e) {
                callback.onFailure(e);
            }
        }

        public void onError(Request request, Throwable exception) {
            callback.onFailure(exception);
        }
    });
    try {
        rb.send();
    } catch (RequestException e) {
        callback.onFailure(e);
    }
}

In your code, it might look like this:

...
    httpGetFile(url, new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
            xmlData = "Error";
        }

        public void onSuccess(String xmlText) {
            xmlData = xmlText;
        }
    }
....

这篇关于使用GWT读取非常大的本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆