Developing Web Widgets
Last night I finally finished the improvements to my xml2json generic handler which were required to develop a Stickam widget. Since I am sending the URL of the feed as a query string value to the generic handler it was necessary to deal with ampersands in the feed's URL without screwing up the query string. So I added a line to replace the | character with the & character. If I have a feed with an ampersand in the URL I'll just send it with the | character instead. Trace listeners don't seem to work in generic handlers so I added code to write the feed URL and the JSON string to a log file for debugging purposes. The Stickam players in the XML were causing invalid JSON syntax because they were not getting null values. I am not interested in that information so I used RegexBuddy to come up with the regular expression to replace them with empty strings. I came across the infamous "invalid label name" error so I added parentheses around my JSON string. The last thing I did was add XML comments for the method that actually converts the XML to JSON because this code should be replace when a more reliable algorithm becomes available:
<%@ WebHandler Language="C#" Class="xml2json" %>
using System;
using System.Web;
using System.Xml;
using System.Text; using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
public class xml2json : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/javascript";
string strFeed;
string strJSON;
strFeed = HttpContext.Current.Request.QueryString["feed"];
// need to replace | with & for query string ampersands
strFeed = strFeed.Replace('|', '&');
FileInfo objFile = new FileInfo(@"C:\\Log\debug.log");
StreamWriter swOutput = objFile.CreateText();
swOutput.WriteLine(strFeed);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(strFeed);
// Convert XML to a JSON string
string JSON = XmlToJSON(xdoc);
// strip out bad JSON syntax
JSON = Regex.Replace(JSON, @"\""player\d\"": \},", "");
// surround the JSON string with parentheses to prevent "label name" errors
JSON = "(" + JSON + ")";
strJSON = "var obj = eval(" + JSON + ");";
swOutput.WriteLine(strJSON);
// release file resources
swOutput.Close();
swOutput.Dispose();
// return the JSON string
context.Response.Write(strJSON);
}
public bool IsReusable {
get {
return false;
}
}
/// <summary>
/// Converts XML to JSON
/// </summary>
/// <param name="xmlDoc">The XmlDocument object to convert</param>
/// <returns>JavaScript Object Notation string</returns>
/// <remarks>Code from http://www.phdcc.com/xml2json.htm</remarks>
private static string XmlToJSON(XmlDocument xmlDoc)
To practice my web design skills I added rounded corners and a diagonal stripe background to the widget. This was purely a coding exercise because I've already developed a Yahoo! Widget for the same purpose. So far I have used this xml2json generic handler for the following; to get my dynamic IP address, to get the weather from the National Weather Service, and to get postage rates from USPS (all using web services that return XML). I have confirmed that this will also work in a compiled HTML file so you can include widgets in your documentation provided you can run the xml2json generic handler on a web server.
I have pre-ordered a new book, Professional Web Widgets with CSS, DOM, JSON and Ajax which promises to cover the topic in depth. I hope it has extensive coverage of JavaScript Object Notation because my other books on AJAX tend to skim over JSON.