Skip to content

Instantly share code, notes, and snippets.

@aconbere
Created November 3, 2009 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aconbere/225553 to your computer and use it in GitHub Desktop.
Save aconbere/225553 to your computer and use it in GitHub Desktop.
build = function(tag, attrs){
var node = document.createElement(tag);
for(var k in attrs) {
node.setAttribute(k, attrs[k])
}
return node;
};
text = function(text) {
return document.createTextNode(text);
};
xml = function(tag, attrs, closure){
var node = build(tag, attrs);
if(closure) {
var scope = {
xml: function(tag, attrs, closure) { node.appendChild(xml(tag, attrs, closure));},
text: function(t) { node.appendChild(text(t)); }
};
closure.apply(scope);
}
return node;
};
/*
var iq = xml('iq', {xmlns: "http:..."}, function() {
this.xml('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: "root"}, function() {
for(var i = 0; i < 10; i++){
this.xml('item', {}, function() {
this.text('item-' + i);
});
}
});
});
output
<iq xmlns="http:...">
<query xmlns="http://jabber.org/protocol/disco#items" node="root">
<item>item+1</item>
<item>item+2</item>
<item>item+9</item>
</query>
</iq>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment