Friday, November 5, 2010

jQuery and Node.js

jQuery makes it really easy to select certain parts not only of a HTML page but also of an XML document. So if you just want to parse some (and not too big) XML file or do not care too much ab the speed of your parser a combination of node.js and jquery can help a lot and make some stuff really less painful.

So what is needed (ubuntu linux):
  • Install node.js on ubuntu:
  • Download jquery (you could also include an online version - google is your friend): I use jquery-1.4.2.min.js (1.4.3 did not work for me)
Then try this example:

Type into a terminal:
> node helloJQueryNode.js

It should write to the console:
> Hello World, It works! Really!

Now a more complex example:

again Type into a terminal:
> node xmlTest.js

It should write to the console:
> 229.29

Pretty simple!! So what does it do? So you have got an XML file. Node js fetches the file and jQuery is used for parsing. Well using jQuery selector syntax to extract the stuff you are looking  for from an XML file is typically a lot easier than for example using XPath.
Okay, as already mentioned for big XML files and if you have lots of files to crawl *I* would use more likely Java (apache httpclient + a fast STAX-parser such as Woodstox). For very simple tasks Node.js + jQuery is really a good choice.

Some more about Node.js: