Skip to content

Instantly share code, notes, and snippets.

@DTrejo
Created April 30, 2011 03:09
Show Gist options
  • Save DTrejo/949362 to your computer and use it in GitHub Desktop.
Save DTrejo/949362 to your computer and use it in GitHub Desktop.
start of bidi conversion between js<-->xml
var util = require('util')
, fs = require('fs')
, xml2js = require('xml2js-expat')
, querystring = require('querystring')
, _ = require('underscore');
// this is provided by xml2js
function parse(xmlString, cb) {
var parser = new xml2js.Parser();
parser.on('end', function(result) {
cb(null, result);
});
parser.on('error', function(err) {
cb(err);
});
parser.parseString(xmlString);
}
// this is what I need to write.
function format(object, parent) {
return formatHelper(object, parent, '\n');
}
function formatHelper(object, parent, tabs) {
if (object) {
var xml = ''
, prenewline = false;
xml += tabs + '<' + parent;
if (object['@']) {
xml += ' ' + querystring.stringify(object['@'], '" ', '="') + '"';
prenewline = true;
}
xml += '>';
delete object['@'];
for (var k in object) {
var v = object[k];
if (_.isArray(v)) {
v.forEach(function(el) {
xml += formatHelper(el, k, tabs + '\t');
});
} else if (_.isString(object)) {
var str = object;
xml += str; //formatHelper(null, parent, tabs + '\n\t');
prenewline = false;
} else if (k == '#') {
xml += v;
prenewline = false;
} else {
xml += formatHelper(v, k, tabs+'\t');
prenewline = true;
}
xml += (prenewline) ? tabs : '';
xml += '</' + parent + '>';
return xml;
}
} else { // should never get here.
console.log('--err', object, parent);
return '';
}
}
// reading xml and looking at it, so we can base the xml2js on this.
fs.readFile('test.xml', 'utf8', function(err, xmlString) {
console.log('==xml==');
console.log(xmlString);
console.log();
parse(xmlString, function(err, object) {
if (err) console.log(err);
console.log('==xml2js==');
console.log(util.inspect(object, false, null));
console.log();
console.log('==xmlString==');
var xml = format(object, 'directededge');
console.log(xml);
// let's see if we did shit right!
parse(xml, function(err, test) {
console.log('worked? (mine then theirs)');
console.log('this should work, but it doesn\'t, oops');
console.log(util.inspect(test, false, null));
console.log(util.inspect(object, false, null));
});
});
});
// var object =
// { '@': { version: '0.1', test: 'ohai' },
// item:
// [ { '@': { id: 'customer1' },
// link:
// [ 'product1',
// 'product2',
// { '#': 'product3', '@': { ohnice: 'test' } } ] },
// { '@': { id: 'customer2', ohnice: 'test' },
// tag: 'customer' },
// { '@': { id: 'customer3' }, link: 'product3' },
// { '@': { id: 'product1' }, tag: 'product' },
// { '@': { id: 'product2' }, tag: 'product' },
// { '@': { id: 'product3' }, tag: 'product' } ] };
// three cases
// hit object
// hit array
// hit string
<directededge version="0.1" test="ohai">
<item id="customer1">
<link>product1</link>
<link>product2</link>
<link ohnice="test">product3</link>
</item>
<item id="customer2" ohnice="test">
<tag>customer</tag>
</item>
<item id="customer3">
<link>product3</link>
</item>
<item id="product1">
<tag>product</tag>
</item>
<item id="product2">
<tag>product</tag>
</item>
<item id="product3">
<tag>product</tag>
</item>
</directededge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment