Steph
Steph

Reputation: 2185

What is the simplest way to read in an XML file using GWT?

I'd like to read in an XML file that is stored on my Tomcat server in a different location than my GWT project. How is this easily accomplished? Should I be making Remote Procedure Calls? Maybe retrieving JSON data via HTTP? I saw this page but am not sure if this is the solution to my problem or if something like RPCs is making this more difficult than it needs to be. I'm a newb, so please steer me in the right direction if I'm going about this all wrong.

I have also looked at this page, which explains how to parse the XML file. I understand the parsing, but I'm having trouble figuring out how to get the XML file off of the server in the first place. I've tried this:

try {
        new RequestBuilder(RequestBuilder.GET, 
                "http://localhost:8180/Videos/testFile.xml").
                sendRequest("", new RequestCallback() {

              @Override
              public void onResponseReceived(Request req, Response resp) {
                String text = resp.getText();
                // This method parses the XML file, but is never getting any data to parse
                parseMessage(text);
              }

              @Override
              public void onError(Request res, Throwable throwable) {
                // handle errors
                  System.out.println("Error occurred");
              }
            });
    } catch (RequestException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But when I try this, parseMessage(text) never gets any data to parse, it just gets an empty string.

Upvotes: 2

Views: 2700

Answers (3)

Dominik Bucher
Dominik Bucher

Reputation: 1509

Make sure your file really is in war/Videos/testFile.xml. Then, a trick for developing and debugging, add + "?uid=" + Math.random() to your filename, otherwise your browser might cache the file, always giving you the same (in this case empty) file.

Could be a same origin issue, but if you run it locally (your server on port 8180), shouldn't be. But be sure that it's the same server you fetch your xml and application from, otherwise they will run on different ports, causing a SOP issue.

Upvotes: 1

Chris Cashwell
Chris Cashwell

Reputation: 22859

Try this:

String url = "http://localhost:8180/Videos/testFile.xml";
// typically it would be a good idea to URL.encode(url);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
    builder.sendRequest(null, new RequestCallback() {

          @Override
          public void onResponseReceived(Request req, Response resp) {
            String text = resp.getText();
            parseMessage(text);
          }

          @Override
          public void onError(Request res, Throwable throwable) {
            // handle errors
              System.out.println("Error occurred");
          }
        });
} catch (RequestException e) {
    e.printStackTrace();
}

If you're still experiencing problems, start debugging the response.

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163312

I think you are likely to run foul of the cross-site scripting rules.

For strange historical reasons that have nothing really to do with security, a client-side application can retrieve JSON data from a host other than its "home site", but it cannot retrieve XML data.

Upvotes: 0

Related Questions