Reading a very large local XML file using GWT
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 ty开发者_StackOverflow中文版pos, 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;
}
}
....
精彩评论