<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bandos&#039; Arcade &#187; Javascript</title>
	<atom:link href="http://www.nuwanbando.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nuwanbando.com</link>
	<description>&#34;It&#039;s not about how it is, but how I see it &#34; - Stranger Than Fiction</description>
	<lastBuildDate>Thu, 02 Feb 2012 08:52:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Web Scraping &amp; Parsing HTML to XML in Javascript</title>
		<link>http://www.nuwanbando.com/2010/04/web-scraping-parsing-html-to-xml-in-javascript/</link>
		<comments>http://www.nuwanbando.com/2010/04/web-scraping-parsing-html-to-xml-in-javascript/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 19:55:50 +0000</pubDate>
		<dc:creator>Nuwan Bandara</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Google gadgets]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[WSO2]]></category>
		<category><![CDATA[WSO2 Gadget Server]]></category>
		<category><![CDATA[WSO2 Mashup Server]]></category>
		<category><![CDATA[Mashup]]></category>

		<guid isPermaLink="false">http://www.nuwanbando.com/?p=412</guid>
		<description><![CDATA[Today I was working on a customer POC and happened to create few Google gadgets to visualize selected data sets from *.gov.uk sites. The scenario which is implemented was, mixed with inter-gadget communication and content search over data.gov.uk sites. I created three simple gadgets which communicates with each other, and one acted as the controlling [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.nuwanbando.com%2F2010%2F04%2Fweb-scraping-parsing-html-to-xml-in-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.nuwanbando.com%2F2010%2F04%2Fweb-scraping-parsing-html-to-xml-in-javascript%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Today I was working on a customer POC and happened to create few <a href="http://code.google.com/apis/gadgets/">Google gadgets</a> to visualize selected data sets from *.gov.uk sites. The scenario which is implemented was, mixed with inter-gadget communication and content search over data.gov.uk sites. I created three simple gadgets which communicates with each other, and one acted as the controlling gadget which pushed the search parameters to other two gadgets. The two content gadgets showed UK (1) primary school information and (2) electoral information. The pushed parameter was the postal code of different parts of UK. The <a href="http://www.direct.gov.uk/en/index.htm" target="_blank">direct.gov.uk</a> has a form based implementation of this.</p>
<p><a href="http://www.nuwanbando.com/wp-content/uploads/2010/04/Screenshot.png"><img class="size-medium wp-image-413 alignleft" title="Screenshot" src="http://www.nuwanbando.com/wp-content/uploads/2010/04/Screenshot-241x300.png" alt="" width="241" height="300" /></a></p>
<p>The Requirements for the POC was, simple and we already had working samples of <a href="http://wso2.org/library/articles/2010/03/wso2-gadget-server-inter-gadget-communication-pubsub" target="_blank">such a scenario</a> at WSO2 library.</p>
<ol>
<li>Show how one gadget can pass the context to other gadgets</li>
<li>How gadgets can harvest data in various formats (in my previous post I explained on how to get data from RDF endpoints, which are also available in *.gov.uk sites)</li>
</ol>
<p>The building blocks for the implementation was the search url, which was quite straight forward. for all the requests based on postal codes the direct.gov site served in the same manner (because of this important fact, the automation process became trivial). for an instance the url for primary school information retrial was,</p>
<p><a href="http://local.direct.gov.uk/LDGRedirect/LocationSearch.do?LGSL=13&amp;searchtype=1&amp;LGIL=8&amp;Style=&amp;formsub=t&amp;text=SE1+7DU" target="_blank">http://local.direct.gov.uk/LDGRedirect/LocationSearch.do?LGSL=13&amp;searchtype=1&amp;LGIL=8&amp;Style=&amp;formsub=t&amp;text=<strong>SE1+7DU</strong></a></p>
<p>Where the param &#8220;text&#8221; changed according to the postal code. So far everything seemed straight forward, however at implementation, while using <a href="http://code.google.com/apis/gadgets/docs/dev_guide.html">Gadgets API</a> for content retrial, I faced problems in parsing text with javascript. Hence the <a href="http://code.google.com/apis/opensocial/docs/0.7/reference/gadgets.io.html#makeRequest" target="_blank">gadgets.io.makeRequest </a>supported HTML as text and the API method returned the retrieved HTML document as string making it quite impossible to process.</p>
<p>With some thinking and advise, I brought the <a href="http://wso2.com/products/mashup-server/">Mashup Server</a> in to the picture and used it to retrieve the data from the gov site and returned the result in XML format. Using the Mashup Server web scraping seems to be a piece of cake, We created a simple mashup using the scraper host-object and captured the result set in the search result page. The mashup code as follows,</p>
<pre class="js" name="code">
function search(searchUrl) {
	var scraper = new Scraper(
		<config>
		    <var-def name="url">{searchUrl}</var-def>
			<var-def name="response">
			    <xpath expression="//div[@id='bodyContent']//ul[@class='resultsList']/li/a">
				<html-to-xml>
				   <http method='get' url='${url}'/>
				</html-to-xml>
			     </xpath>
			</var-def>
		</config>
	);
	return new XMLList(scraper.response);
}
</pre>
<p>And finally the two gadgets were making service calls to the mashup service and retrieved the data as an XML object, making the data processing painless. The final version at the Gadget Server looked quite appealing.</p>
<div id="attachment_427" class="wp-caption aligncenter" style="width: 829px"><a href="http://www.nuwanbando.com/wp-content/uploads/2010/04/gs.png"><img class="size-large wp-image-427 " title="gs" src="http://www.nuwanbando.com/wp-content/uploads/2010/04/gs-1024x509.png" alt="WSO2 Gadget Server with UK gov data" width="819" height="407" /></a><p class="wp-caption-text">Gadget Server look - in the end</p></div>
<p>Special thanks goes to <a href="http://ruchirawageesha.blogspot.com/">Ruchira</a> for helping me out with the mashup service <img src='http://www.nuwanbando.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You can <a href="http://www.nuwanbando.com/wp-content/uploads/2010/04/wso2gs-samples.zip">download</a> the Gadget code and the Mashup service and try the scenario yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nuwanbando.com/2010/04/web-scraping-parsing-html-to-xml-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

