Wednesday, December 05, 2007

XML to JSON

Recently, I came upon the need to provide AJAX support for a site where the XML data comes from a remote site.

Security settings on the browsers prevent making a request to a non-originating server so, the solution is to create a proxy servlet.

Also, parsing XML generally sucks, parsing it with javascript is worse. To deal with this, the ideal solution is to return JSON instead of XML.

So, with a combination of the Jakarta Commons HttpClient (its just more robust than the java.net.HttpURLConnection), a cool schema to convert xml to json, the quintessential xml/xslt transformer, and a little glue, a solution is born.

Some things to note.
Use the version of the schema in the issues part of the Google Code page. It contains some useful bug fixes.
The transformer should work on streams instead of files (of course).
Be smart about thread handling with the HttpClient (cache results to minimize use of the HttpClient if you expect any kind of serious traffic).
Be sure the XML file is encoded the way you expect (UTF-8, ISO-8859-1, or whatever). If you get some crappy, I mean mis-encoded data, you can fix it when creating the InputSource:
Source source = new StreamSource(
   new InputStreamReader(xmlIn,"ISO-8859-1"));


For unit/integration testing, I highly recommend using the Rhino JavaScript interpreter. You can feed the JSON and your script to it to ensure things work the way you expect. A little jUnit to run things and life is good.

Labels: , , , ,

permalink
Links to this post
1 comments

Wednesday, June 13, 2007

AJAX + DOJO

This morning I attended the session on AJAX and DOJO (OC10). It was beyond packed. Every available seat and place on the floor was taken. That should really be a sign of peoples interest in the subject.

The session was presented well, we got to see source code, and there was a lot of information on how this can integrate with other things. They were good about pointing out the limitations of the technology, as well as providing tips on its use. There was also a fairly good discussion about accessibility and section 508 compliance.

I was hoping to bring up a little more technical detail but there really wasn't time (of course, I also wanted to shamelessly plug my dojo tutorials).

Really, the only thing lacking was, I would have preferred to see more live demos than screen shots of web browsers. Ahh, just another demonstration of how evil powerpoint is in a presentation.


_

Labels: , ,

permalink
Links to this post
0 comments