Skip to content
Browse files

Add parsers

  • Loading branch information...
1 parent db3469d commit fb93a64ef3ced57d5d735077156978520c686e0c @pfleidi committed May 1, 2011
Showing with 47 additions and 0 deletions.
  1. +47 −0 lib/wwwdude/parsers.js
View
47 lib/wwwdude/parsers.js
@@ -0,0 +1,47 @@
+/*!
+ * Available parsers for automagical content parsing
+ *
+ * Copyright(c) 2011 Sven Pfleiderer.
+ * MIT Licensed
+ *
+ * @author pfleidi
+ */
+
+/*
+ * Dependencies
+ */
+var Util = require('util');
+
+/**
+ * Content parser wrappers
+ *
+ * @return {Object}
+ */
+module.exports = {
+
+ json: function json(content, callback) {
+ try {
+ if (content) {
+ var parsed = JSON.parse(content);
+ callback(null, parsed);
+ } else {
+ callback(null, {});
+ }
+ } catch (err) {
+ callback(err);
+ }
+ },
+
+ xml: function xml(content, callback) {
+ var Xml2Js = require('xml2js-expat');
+ var xmlParser = new Xml2Js.Parser(function (result, error) {
+ if (error) {
+ callback(error);
+ } else {
+ callback(null, result);
+ }
+ });
+ xmlParser.parseString(content);
+ }
+
+};

0 comments on commit fb93a64

Please sign in to comment.
Something went wrong with that request. Please try again.