Hi. Im having a problem using RequestBuilder. Instead of using an
inner class of type RequestCallBack, I created a class that implements
it. Here's what I've done,
1)Created a utility class named XmlParserUtil. This will return a
parsed XML document. (Document object)
2)Inside, there's 1 method,getDocument and 1 inner class,
XmlRequestCallback. The former returns the parsed XML document object,
the latter implements RequestCallback interface and has 3 methods, the
2 implemented methods and 1 method I created, getDocument() which is
called by XmlParserUtil.getDocument and returns the parsed document.
3)I instantiate XmlParserUtil and a Document object on my entrypoint
on the onModuleLoad method, so that the document object takes in the
value on XmlParserUtil's getDocument method.
Now, I get NullPointerException. I checked it and it seems that my
XmlParserutil.getDocument returns a null document object.
I placed a print statement on the onResponseReceived() method. I also
placed a print statment on the getDoc() class of the inner class so it
prints everytime it is called. But when i run the program, the
getDoc()'s print get exceuted before the onResponseReceived()'s print,
so naturally the returned string of getDoc() is null. they both print
xmlStrReq, the string form of the xml file.
Could you help me? Here's my code:
XmlParserUtil.java :
package com.trylang.pol.client.util;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;
public class XmlParserUtil {
String url;
Document doc;
RequestBuilder requestBuilder;
private String xmlStr;
//on instantiation, you must provide the xml file name to be
parsed
public XmlParserUtil(String xmlFileName) {
this.url = xmlFileName;
}
public Document getDocument() {
//created an instance of the inner class located after
this method
XmlRequestCallBack xmlRcb = new XmlRequestCallBack();
try {
requestBuilder = new
RequestBuilder(RequestBuilder.GET,url);
requestBuilder.sendRequest(null,xmlRcb);
doc = xmlRcb.getDoc(); //calls xmlRcb's getDoc
method, w/c returns
a parsed xml
//
doc.
}
catch (RequestException ex) {
GWT.log(ex.getMessage(), ex);
}
return doc;
}
//a class that implements the requestcallback interface
class XmlRequestCallBack implements RequestCallback {
private String xmlStrReq;
private Document docReq;
public void onError(Request request, Throwable exception) {
}
public void onResponseReceived(Request request, Response
response) {
//gets the text form of the xml file, then parses it.
xmlStrReq=response.getText();
docReq = XMLParser.parse(xmlStrReq);
System.out.println(xmlStrReq);
}
public Document getDoc() {
System.out.println(xmlStrReq);
return docReq;
}
}
}
The entrypoint's onModuleLoad() method:
public void onModuleLoad() {
........
XmlParserUtil xmlParser = new XmlParserUtil("image_uris.xml");
Document doc = xmlParser.getDocument();
String subject =
doc.getElementsByTagName("Image").item(0).getFirstChild().getNodeValue();
String subject2 =
doc.getElementsByTagName("Image").item(1).getFirstChild().getNodeValue();
.......
im very sorry for the long post. ive been tinkering with my codes for
almost an hour and I cant fix it. thanks!
On Nov 5, 12:34 pm, Sammi <[EMAIL PROTECTED]> wrote:
> Hi mives29,
> I just solve the problem. It's quite direct and easy.
>
> 1. upload your xml file to any webserver, e.g.http://a.b.c/test.xml
>
> 2. use RequestBuilder to get it. your url.
>
> 3. String xmlStr = response.getText();
>
> 4. use XMlParser to parse the xmlStr. that's it. Sample snippet code
> here:
>
> String url = "test.xml";
> RequestBuilder requestBuilder = new
> RequestBuilder(RequestBuilder.GET,url);
> try {
> requestBuilder.sendRequest(null, new RequestCallback() {
> public void onError(Request request, Throwable exception) {
> }
> public void onResponseReceived(Request request, Response
> response){
> String xmlStr=response.getText();
> //Parse it then.
> }
> }
> );
> } catch (RequestException ex) {
> GWT.log(ex.getMessage(), ex);
> }
>
> Hope that helps
> Sammi
>
> On Nov 4, 7:24 pm, mives29 <[EMAIL PROTECTED]> wrote:
>
> > Hi. I'm a newbie in GWT, and I've been tasked to create a utility
> > class to parse xml files that the contents would be used in layouting
> > the widgets in its container panel (along with other UI
> > configurations).
>
> > I've accomplished this by using a service, which uses Java's DOM style
> > of parsing. This service returns an array list of serializable objects
> > that I created (a POJO that stores the configurations). Then I use the
> > POJOs inside that array list to set various stuff in the UI (xpos,
> > ypos, etc) for different widgets.
>
> > Then I've found out that GWT has the xml api, that I can use to parse
> > my XML. However, XMLParser .parse() only accepts the string
> > representation of the actual XML file. I thought that I could do the
> > same thing I did with my service implementation (using
> > documentbuilder.parse(path-to-xmlfile).
>
> > Now I'm thinking of creating a service that would return a string
> > representation of the XML file, then send it to my GWT class and let
> > that class handle the parsing. Is this possible? or is this even
> > acceptable?
>
> > Ive been seeing stuff on the net using HttpRequest(or RequestBuilder?)
> > to get XML from a script, but I dont know how to do that(the example
> > was on php.)
>
> > Any idea how can I implement this properly? Thanks
>
> > mives29
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---