Reading XML on Client

16 views
Skip to first unread message

Pints

unread,
May 14, 2009, 6:20:38 AM5/14/09
to Google Web Toolkit
Hi,

Can any share the ideas that how can i read XML on client in GWT?

thanks,

abhiram wuntakal

unread,
May 14, 2009, 6:24:41 AM5/14/09
to Google-We...@googlegroups.com
I dont think so u can do any such operations on the client side. You can rather use the file upload widget, transfer the code to the sever side and make use of simple File Read opeartions to read the contents of the XML file. Then you can bring back this data and display it on the client side.

hope this helps.

regards,
Abhiram

Paul Robinson

unread,
May 14, 2009, 6:33:12 AM5/14/09
to Google-We...@googlegroups.com

Rohit Vadera

unread,
May 14, 2009, 7:11:56 AM5/14/09
to Google Web Toolkit
Ok i gottcha.

But would it be fast enough compared to RPC which will use SAX for
parsing and then returning the data structure of values in collection.
Does it really depends on XML size to choose between GWT API or SAX on
server??

Thoughts!!!!!

On May 14, 3:33 pm, Paul Robinson <ukcue...@gmail.com> wrote:
> http://code.google.com/webtoolkit/doc/1.6/DevGuideCodingBasics.html#D...
>
>
>
> abhiram wuntakal wrote:
> > I dont think so u can do any such operations on the client side. You
> > can rather use the file upload widget, transfer the code to the sever
> > side and make use of simple File Read opeartions to read the contents
> > of the XML file. Then you can bring back this data and display it on
> > the client side.
>
> > hope this helps.
>
> > regards,
> > Abhiram
>
> > On Thu, May 14, 2009 at 3:50 PM, Pints <rohitvad...@gmail.com
> > <mailto:rohitvad...@gmail.com>> wrote:
>
> >     Hi,
>
> >     Can any share the ideas that how can i read XML on client in GWT?
>
> >     thanks,- Hide quoted text -
>
> - Show quoted text -

Salvador Diaz

unread,
May 14, 2009, 7:27:12 AM5/14/09
to Google Web Toolkit
Hi,

> But would it be fast enough compared to RPC ?

AFAIK, speed will wildly vary between browsers. IMHO you're better off
parsing the xml server-side and returning a java object to your
client. plus you get to use awesome frameworks like XStream:
http://xstream.codehaus.org/

In our GWT applications we make heavy use of REST calls that return
xml. That xml is then parsed with xstream and it can be as simple as:

XStream xstream = new XStream();
MyObject object = (MyObject)xstream.fromXML(myXmlRestResponse);

Hope that helps,

Salvador

Rohit Vadera

unread,
May 14, 2009, 8:01:46 AM5/14/09
to Google Web Toolkit
I would be giving the try to XStream.

But before that

http://code.google.com/webtoolkit/doc/1.6/DevGuideCodingBasics.html#D...

The example given by google in above link never tells how we have to
read the xml from file system becasue as I know java.io is again not
supported.

Once read then only i can convert it into String.

Anybody clues please!!!
> > > - Show quoted text -- Hide quoted text -

Salvador Diaz

unread,
May 14, 2009, 9:45:47 AM5/14/09
to Google Web Toolkit
You simply can't directly read an xml file located at the client-side
filesystem without uploading it first to your server.

So the steps to read - deserialize xml, when that xml is in the
filesystem of the client is:
1. upload file to server. Using a normal java servlet for example (or
with your preferred method if you have another one)
2. Once the xml is in your server, you deserialize it with your
framework of choice (xstream for example)
3. Once you have populated objects with the values from the xml, you
can notify the client (in the reponse from your upload servlet for
instance)
4. The client is notified and calls the server to retrieve the
deserialized object.

It's a little more convoluted than just making a RPC but any java
developer worth its salt should be able to do it. For a
FileUploadServlet example take a look at:
The servlet code:
http://code.google.com/p/hellagwt/source/browse/trunk/hellagwt/src/main/java/fr/salvadordiaz/gwt/hellagwt/server/NzbUpload.java
The client code:
http://code.google.com/p/hellagwt/source/browse/trunk/hellagwt/src/main/java/fr/salvadordiaz/gwt/hellagwt/client/views/UploadPanel.java

That should help, if it doesn't I don't know what could

Salvador

Alyxandor

unread,
May 14, 2009, 11:31:37 PM5/14/09
to Google Web Toolkit
If you aren't worried about modifying your xml, one way to do it
WITHOUT servlets {as java servers are not always available} is to read
the file using GWT HttpRequest {must ensure text/plain content-type,
more later...}, Parse it into XML, then translate that into JSNI or
Java Objects. I prefer JS objects, because I need all my data to have
module-indepenant organization for cross-frame hack, but you can do it
however you like. I'll post some of the source I use, don't try to
copy+paste this, it won't even come close to working, it's just here
as one example of client-side xml parsing. You can check the actual
use and the references used at http://aiyx.info/xSrc.html#xSource/xBook/xJAXB/client/xUnmarshallor.java

I use .htaccess to set the xml file's content type to text/plain, you
can use php, java or whatever you like...

The main idea is that I request the xml files with a Url to the server
hosting the xml {must be the same server as the host page unless you
use Ray Cromwell's window.name hack}, and a callback object that
accepts whatever kind of data type I turn the xml nodes into...

This code is just to give a general idea...




private static String xLastId="0$";
public static final String xParseXMLNode(Node x){

switch(x.getNodeType()){
case Node.ELEMENT_NODE:

xDNA xRet = xAxon.xCreate();//This is my JSNI data object, don't
worry about them.
xRet.xSetStr(xNS.xECUTABLE+xNS.xID, xLastId);//String reference to
parent object
String name = x.getNodeName().toLowerCase();//Node name
xRet.xSetInt(xNS.xTYPE, xGenome.xNodeNameMachina(name));//Turn
node names into ints
xGenome.xNodeNameHumana(xRet,name);//Make sure system knows how to
turn them back
if (x.hasAttributes()){
NamedNodeMap xAttrs = x.getAttributes();
for (
int i =xAttrs.getLength();
i-->0;
xApplyAttr(xAttrs.item(i),xRet)//Record attributes for later
);

}
if (x.hasChildNodes()){
NodeList xCore = x.getChildNodes();
for (
int i = -1;
++i<xCore.getLength();
xRet.xAdd(xParseXMLNode(xCore.item(i))) //Recurse....
)xLastId=xRet.xid();//Keep track of parent id
}
return xRet.xid(); //Returns string reference, xDNA is stored
globally and retrieved as needed
case Node.TEXT_NODE:
String xTxt; //This is basically a test to skip all-whitespace
nodes, but preserve whitespace elsewise
if (xRegulars.xRegNonWhiteSpace.test((xTxt=xCommons.xTrim
(x.getNodeValue())))){
return xDendrite.xCreate(xTxt).xid(); //Returns an object like
xDNA, except toString() = text node value
}
}
return null;
}

//This function iterates through the attributes, and sets them to JSNI
without converting to Java objects
public static void xApplyAttr(Node _,xDNA x){
Node val = _.getFirstChild();
if (val!=null){
String name = _.getNodeName().toLowerCase();
int type = xGenome.xAttrNameMachina(name); //Use integers instead
of strings, they're faster
xGenome.xAttrNameHumana(type, name);//Make sure we can go back to
strings later
x.xPushAttrRef(type);
//Default / unregistered attributes stored as string, all number /
boolean types must be declared BEFORE hand...
switch(xGenome.xGetAttrForm(type)){
case xGenome.xTYPE_BOOLEAN://Accepts "true" or "1", and "false" or
"0"
if (val.getNodeValue().toLowerCase().equals("true"))
val.setNodeValue("1");
if (val.getNodeValue().toLowerCase().equals("false"))
val.setNodeValue("0");
case xGenome.xTYPE_INT:
x.xSetInt(xNS.xPrefixAttrName+type, Integer.parseInt
(val.getNodeValue()));
break;
case xGenome.xTYPE_DOUBLE:
x.xSetNum(xNS.xPrefixAttrName+type, Double.parseDouble
(val.getNodeValue()));
break;
case xGenome.xTYPE_STRING:
default:
x.xSetStr(xNS.xPrefixAttrName+type,val.getNodeValue());
}
}
}

public static xDNA xCreate(String x){

Document xNew = XMLParser.parse(x);
if (xNew==null)
return JavaScriptObject.createObject().<xDNA>cast();

XMLParser.removeWhitespace(xNew.getDocumentElement());
return xDNA.xGet(xParseXMLNode(xNew.getDocumentElement()));
}
//xJSXCallback is an interface that accepts xDNA
public static void xRead(final String url,final xJSXCallback xToDo){
if (xLocus.xHas(url)){//TODO maybe put back xUserAgent.xStandards()
xToDo.xProcess(xLocus.<xDNA>xGet(url,xDNA.xGet("0$")));
}
else
xXmlReq.xRequest(url, new xXmlCallback(){//XmlCallback just get's
the text with HttpRequest and parses into XML Document
public void xReceive(Document x) {
XMLParser.removeWhitespace(x);
xParse(url, x.getDocumentElement(), xToDo);

}
});
}


protected static void xParse(String url,Node xEl,xJSXCallback xToDo){
String machina = xParseXMLNode(xEl);
xDNA xSrc = xDNA.xGet(machina);
if(url.length()>0)
xLocus.xSet(url, xSrc);

xToDo.xProcess(xSrc);
}


...If you want to see how this actually works, check out
logickmal.appspot.com, click a link to open an iframe, right-click to
make iframe top level, inspect in firebug, check the $wnd.X variable
to see how the data is parsed, and press Ctrl+U to see the document's
source. I embed the xml into the host page for SEO purposes...

You don't have to use my methods, but this loop should parse any xml
document into some kind of object. I'm working on a way to do all the
HttpRequest and Xml parsing in an iframe on a different server than
the host page, and return it for conversion into UI using the
window.name hack. ...I'll probably put it up as a code.google group
when it's ready. Until then, good luck!
Reply all
Reply to author
Forward
0 new messages