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

Tuesday, June 12, 2007

XForms Mojo

I visited what was probably the best presentation in a session on XForms. It was presented by Doug Tidwell, of XSLT fame. It was entertaining and informative.
I was able to find my answer to what is being sent over http during a submission, HTTP PUT. But, since it is a parameter I'm sure get and post are also supported.

One really interesting usage is creating a custom editor for an XML file. The file itself can be used to generate the XFORM.

One of the demos had to perfect circle:
XForm -> XML -> XSLT -> XForm ..........

This has me think, if I can find a java based renderer for xforms, then I could make a universal XML editor as an Eclipse plugin. Considering the amount of xml we have to deal with in the J2EE world, there could be a lot of potential for such a tool.

Links:
http://formfaces.com (uses javascript to interpret xforms in non-compliant (read all) browsers)
http://chiba.sourceforge.net/ (a standard implementation for the server, with HTML fallback)
http://www.orbeon.com/ (extension of Chiba, to make common things easier)

To Investigate:
XForms / Struts integration
- custom tags?
- chiba?
Eclipse Plugin
- with formfaces?


_

Labels: , ,

permalink
Links to this post
1 comments