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!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---