Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
JavaScript
Branch: master

Merge pull request #8 from oneironautics/master

Added ability to pass options to libxml.
latest commit 53f42aa3b2
@vgrichina authored

README.md

libxmljs-easy

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

Build Status

Installation

 npm install libxmljs-easy

Usage

Note that latest version requires Node.js 0.11.x or 0.12.x and --harmony-proxies option. 0.1.x versions of this module used node-proxy module instead.

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>' +
                      '</book></books>');

Select elements from collections explicitly

 assert.equal(xml.book[0].$name, "Lord of the Rings");
 assert.equal(xml.book[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(xml.book.$name, "Lord of the Rings");
 assert.equal(xml.book.author.$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(xml.book.language[0].$.text(), "English");

Further info

Something went wrong with that request. Please try again.