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:

At 10 December, 2007 14:33, Blogger doyle said...

One thing we are doing is taking XML in and using BETWIXT to serialize to POJO then pushing that into DWR which does the json for you. Sooooo much simpler than parsing.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

Links to this post on: