28 10, 2013

Parsing xml feeds with javascript

By | 2016-11-22T09:52:31+00:00 October 28th, 2013|Categories: Web Development|Tags: , , , |

There can be any number of reasons to parse xml. Perhaps you want to set up a custom search of an RSS feed. Maybe you need to render a data report from your ERP system on a web page but can’t get an html output from your ERP provider. Whatever the reason, parsing xml with javascript is very easy.

To start you’ll need three things. For simplicity we’ll include jQuery 1.5 or later. Next download and include this xml to json converter: http://www.fyneworks.com/jquery/xml-to-json/. Finally you’ll need some xml. My examples will use the following xml feed:

We’ll say my xml is hosted at www.mydomain.com/musicians.xml. In order to search this file you’ll first pull the file into scope with a jQuery get request like this:

That will pass the xml from the file requested as a parameter to the callback function which can be defined either inline as in the example above or as a standalone function for a more modular approach. Inside the callback you’ll need to convert the xml into a json object for easy manipulation. This is done via the following line of code:

At this point the rest is very straight-forward. The xml is now in a json object and can be accessed by the following ways:

Now you have access to all the data in the xml as a single object making data retrieval very simple, even for complex xml data feeds like RSS.

8 02, 2013

Using .Net ASHX Handlers To Build Web Services

By | 2016-04-21T10:04:11+00:00 February 8th, 2013|Categories: Web Development|Tags: , , , |

I was recently given the task of rewriting an existing web service with a predefined SOAP request and response.  I didn’t have access to the original code to use as a reference point, but it was fairly simple in concept:  Receive an incoming SOAP message, do some processing, and send a SOAP response back to the caller.  Initially, I attempted to use a standard Visual Basic ASMX web service but was unable to get my incoming and outgoing objects to perfectly match the predefined SOAP messages that I was expected to use.  I even tried using Microsoft’s WSDL.exe to reverse engineer the old web service’s WSDL (web service definition language) but still had no luck.

After some head scratching, I finally decided to try a more “brute force” approach using a Microsoft HTTP handler (ASHX) file.  Below is a code sample of accepting an incoming SOAP/XML message with one of these ASHX files and sending a response back to the caller:

As you can see, I am simply taking what is being passed in the input stream and loading it in to an XmlDocument object.  From there I can read the XML and do whatever processing is necessary.  Additionally, I am sending a SOAP response back to the client by simply setting the content type of the response to text/xml and writing out the XML.

By setting up my web service using an HTTP handler, I was able to bypass the mysterious Microsoft “black box” that you encounter when using a standard ASMX to send and receive SOAP messages.  And since this places the incoming and outgoing SOAP messages completely under my control, I am able to easily configure the structure of those messages via code.  So at the end of the day I had a functional, scalable, and easily maintained web service.