Wednesday, December 27, 2006

Serialize JavaScript objects to XML (for use with Ajax)

The following 3 functions can be added to any JavaScript library to serialize objects to XML for use with Ajax.

--- serialize.js ---

Array.prototype.toXml = function(t){
var s = new Array(), i, l = this.length, v;
var t2 = (t.charAt(t.length-1)=='s')?t.substring(0,t.length-1):t;

for(i=0;i<l;i++){
v = this[i];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':break;
case 'object':if(v!=null){s.push(v.toXml(t2));}break;
case 'string':v = v.toXml();
default:s.push('<'+t2+'>'+v+'');
}
}
if(s.length>1)return '<'+t+'>'+s.join('')+'';
return s;
};

Object.prototype.toXml = function(t){
var sa = new Array(''), se = new Array('');
if(!t) t=this._tagName||'object';

for(var i in this){
if (this.hasOwnProperty(i) && i.charAt(0)!='_') {
var v = this[i];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':break;
case 'object':if(v!=null){se.push(v.toXml(i));}break;
case 'string':v = v.toXml();
default: sa.push(' '+i+'="'+v+'"');
}
}
}
var s = se.join('');
return '<'+t+sa.join('')+((s!='')?'>'+s+'':'/>');
};

String.prototype.toXml = function(){
return this.replace('&','&amp;').
replace('<','&lt;').replace('>','&gt;').
replace('\'','&apos;').replace('"','&quot;');
};


Sample Usage

<script language="javascript" src="serialize.js"></script>
<script language="javascript">

function Car(make,model,color){
this.make = make;
this.model = model;
this.color = color;
}

var cars = new Array();
cars.push(new Car('BMW','545i','Silver'));
cars.push(new Car('Toyota','Corrola','Red'));
cars.push(new Car('Honda','Accord','Black'));

//serialize to xml
alert(cars.toXml());
</script>


Note:
1. Private properties are prefixed with an underscore
2. You can add a default TagName for an object using the this._tagName property. For example in the Cars object above, you can add:
this._tagName = 'car';
...

Thursday, October 12, 2006

Why use FireFox when you can use Opera?

For the past couple of days I've been getting increasingly frustrated with FireFox because it eats up all of my memory. The tabbed browsing feature is a neccessity for me (I dare not install IE7). So I switched a couple of days ago to Opera which has all the features I need and a very small footprint. After two day, I can say nothing but wonder, Why use FireFox when you can use Opera?

Thursday, September 28, 2006

In Stevey's word on Agile Programming

Up until maybe a year ago, I had a pretty one-dimensional view of so-called "Agile" programming, namely that it's an idiotic fad-diet of a marketing scam making the rounds as yet another technological virus implanting itself in naive programmers who've never read "No Silver Bullet", the kinds of programmers who buy extended warranties and self-help books and believe their bosses genuinely care about them as people, the kinds of programmers who attend conferences to make friends and who don't know how to avoid eye contact with leaflet-waving fanatics in airports and who believe writing shit on index cards will suddenly make software development easier. ... more..
(I kind of agree)