CONT: Those interested in XML/XSLT/XPath

23 views
Skip to first unread message

Eric B

unread,
Feb 5, 2007, 10:35:36 PM2/5/07
to Google Web Toolkit
[This is a continuation of http://groups.google.com/group/Google-Web-
Toolkit/browse_frm/thread/e77f05af3ea4d732/31dfe2c1e3d1fa4b since it's
now closed]

> Is there any chance you could give some more detailed instruction on
> what to do with the GWT wrapper once you've downloaded it? I'm
> completely lost...where do I extract it to? How do I use it in an application?
>
> Thanks!

First off, the link, http://www.ebessette.com/gwt.sarissa.zip, will be
dead shortly as I'm closing down my hosted server for a little while,
so get it while you can.

After downloading the zip file, just extract it into your source
directory for your GWT project and the compiler should pick it up
without any problems.

I've gotten a lot of reports that this code isn't working. It might
be that Sarissa has upgraded and the new version is not backwards
compatible. Also, if you're creating the XML file, it will probably
be faster to build your data in JSON as JavaScript can read it
natively through its code syntax. However, I know some people are
getting XML from a third-party source and it would be less efficient
to transform it into JSON and then to parse the XML using the
browser's native XML and XSLT functions. Plus there isn't, as far as
I know, a transformation language for JSON like XSLT.

In any case, here's an example for using the API I provided:
/*
* Notes
* - I wrote this free hand, so I may have made syntax errors
* - Normally you wouldn't put all the parsing and processing code in
the RequestCallback
*/
RequestBuilder rb = new RequestBuilder( RequestBuilder.POST, "path/to/
xml/file/xmlfile.xml" );
rb.setHeader( "Content-Type", "text/xml; charset=utf-8" );
try {
rb.sendRequest( "", new RequestCallback() {

/*
* (non-Javadoc)
* @see
com.google.gwt.http.client.RequestCallback#onResponseReceived(com.google.gwt.http.client.Request,
* com.google.gwt.http.client.Response)
*/
public void onResponseReceived( Request request, Response response )
{

if ( response.getStatusCode() != STATUS_CODE_OK ) {
// Capture error
return;
}

try {
// Example of parsing the xml content into native DOM
Node xmlDoc = XML.parse( response.getText() );

// Example of finding a list of nodes
List nodes = xmlDoc.getNodes( "/xpath/to/nodes" );
for ( Iterator ni = nodes.iterator(); ni.hasNext(); ) {
Node n = (Node) ni.next();
System.out.println( n.getNodeName() + " = " +
n.getStringValue() );
}

// Example of finding one node or the first noe
Node node = xmlDoc.getNode( "/xpath/to/nodes" );

// Example of applying a XSL file (assuming it was downloaded
before hand)
String transformedText = XSLT.process( xmlDoc, xsltDoc );
System.out.println( transformedText );
}
catch ( XMLParseException xmlpe ) {
// Handle exception
}
catch ( XSLTParseException xsltpe ) {
// Handle exception
}
}
} );
}
catch ( RequestException re ) {
// Handle exception
}

Hope this helps,
Eric

Reply all
Reply to author
Forward
0 new messages