Componentix logo

Componentix blog

Here we write some random thoughts and articles about software development: Java, Grails, Node.js, iPhone, iPad and more.

Work with libxmljs DOM as with plain Javascript objects, similar to E4X


libxmljs-easy is a Node.js module which simplifies XML traversing, similar to E4X.


npm install libxmljs-easy


Use module

var easy = require("libxmljs-easy");

Parse XML

var xml = easy.parse('<books><book name="Lord of the Rings">' +
                         '<author name="J. R. R. Tolkien" />' +
                         '<language>English</language>' +

Select elements from collections explicitly

assert.equal([0].$name, "Lord of the Rings");
assert.equal([0].author[0].$name, "J. R. R. Tolkien");

Use shorthands (works well for case when there is single child element with given name)

assert.equal($name, "Lord of the Rings");
assert.equal($name, "J. R. R. Tolkien");

Basically the idea is that you construct a path from tag names, which can optionally end with attribute name prefixed with “$”.

When index is ommited – the array of elements is matched. When attribute is accessed on such array, its value is concatenated string of attribute values for each of elements in the array.

There is also original DOM element available as “$” property
of individual converted elements.

assert.equal([0].$.text(), "English");

Further info

File uploads using Node.js: once again

I’ve already wrote an article about file uploads in Node.js before. However Node.js is in rapid development and so many things have changed since then.

So, I decided to do an updated version, which accommodates for such changes:

  • events module was removed
  • posix module was replaced by fs
  • various changes into HTTP request/response API
  • multipart module refactored into separate project
  • convenient fs.createWriteStream instead of low-level API

See the code snippet under the cut, or on github. Special thanks goes to Felix Geisendörfer for some useful suggestions.

Please note that you’ll need to install multipart module for the code to work.

To make it clear, Node.js version used was v. 0.1.91.

UPD: Fixed stream.onData handler. It was writing corrupted data (UTF-8 character were translated) because of binary mode not being used. Thanks to Ron Ward for suggesting solution.


File uploads using Node.js: now for real


Node.js is a very interesting server-side Javascript application framework. It is based on Google’s V8 Javascript engine and implements evented I/O, so it is blazing fast. It looks very promising for certain types of applications, mostly those that require long-running connections. This includes streaming big files, keeping persistent connections used for realtime communication (chat, games), applications using third-party web services (with long call times), etc.

There is good enough API reference available for Node.js, however it is not trivial to start development as event-based approach is quite unusual compared to commonly used thread-based networking. There are some useful blog posts with code samples available in Debuggable blog, e.g. Streaming file uploads with node.js.

However the given examples have problem — they are too far from real world problems. Take file upload as example — the code sample provided has absolutely no info on how to save the uploaded file. It is assumed that it would be trivial for the reader to figure out, however it’s not when we have to follow evented I/O approach.

In this post I’ll show example of how to save the uploaded files with Node.js. For the impatient — just grab source from github repository.

UPD: The code here is obsolete, see new article about file uploads in new Node.js version.

Following e-mail is only for robots (never send anything to it, or you would be blacklisted):