XML Module
var xml = require("xml");
The xml
module lets you easily handle XML with a jQuery-like syntax.
The module exports a single method, xml.parse(string)
, that will return a parsed xml document.
You can use find
on the document to search with a CSS selector syntax and text
to get the text contents of a node. Here’s a small example that uses the http library to fetch the RSS feed of the Webpop blog and then use the xml library to find title, description, permalink and publish date for the latest post:
var xml = require("xml"),
http = require("http"),
url = "http://www.webpop.com/blog.rss";
exports.first_post = function() {
var body = http.get(url),
post = xml.parse(body).find("item")[0];
return {
title: post.find("title").text(),
description: post.find("description").text(),
permalink: post.find("link").text(),
published_at: post.find("pubDate").text()
};
};
A template using this extensions, saved as feed.js could look something like:
<pop:feed:first_post>
<h2>
<a href="<pop:permalink/>"><pop:title/></a>
<small><pop:published_at format="time_ago_in_words"/> ago</small>
</h2>
<pop:description wrap="p"/>
</pop:feed:first_post>
Continue to HTML Module »