To facilitate svg editing you can use an intermediate function:
function getNode(n, v) {
n = document.createElementNS("http://www.w3.org/2000/svg", n);
for (var p in v)
n.setAttributeNS(null, p, v[p]);
return n
}
Now you can write:
svg.appendChild( getNode('rect', { width:200, height:20, fill:'#ff0000' }) );
Example (with an improved getNode function allowing camelcase for property with dash, eg strokeWidth > stroke-width):
function getNode(n, v) {_x000D_
n = document.createElementNS("http://www.w3.org/2000/svg", n);_x000D_
for (var p in v)_x000D_
n.setAttributeNS(null, p.replace(/[A-Z]/g, function(m, p, o, s) { return "-" + m.toLowerCase(); }), v[p]);_x000D_
return n_x000D_
}_x000D_
_x000D_
var svg = getNode("svg");_x000D_
document.body.appendChild(svg);_x000D_
_x000D_
var r = getNode('rect', { x: 10, y: 10, width: 100, height: 20, fill:'#ff00ff' });_x000D_
svg.appendChild(r);_x000D_
_x000D_
var r = getNode('rect', { x: 20, y: 40, width: 100, height: 40, rx: 8, ry: 8, fill: 'pink', stroke:'purple', strokeWidth:7 });_x000D_
svg.appendChild(r);
_x000D_