Skip to content

Instantly share code, notes, and snippets.

@mcamiano
Created December 21, 2011 17:30
Show Gist options
  • Save mcamiano/1506879 to your computer and use it in GitHub Desktop.
Save mcamiano/1506879 to your computer and use it in GitHub Desktop.
Convert javascript objects to xml
// note: no error checking is performed at all
// note: no entity substitution (< and & in particular) is performed
// format an element start tag with optional attributes
// gi is assumed to be a generic identifier
// attlist is assumed to be an associative array indexed by attribute name
// eg { myprop1: "val1", myprop2: "val2" }
var formatStag = function(gi, attlist)
{
var data = "<" + gi ;
for (var prop in attlist)
{
data = data + formatAttribute(prop, attlist[prop]);
}
return( data + ">" );
};
// format an attribute
var formatAttribute = function(attname, attval)
{
if (!attval) return "";
return(" " + attname + "='" + attval + "' ");
};
// format an empty element
var formatEElement = function(gi, attlist)
{
var data;
data = "<" + gi + " ";
for (var prop in attlist)
{
data = data + formatAttribute(prop, attlist[prop]);
}
return( data + "/>\n" );
};
// format an element end tag
var formatEtag = function(gi)
{
return("<" + "/" + gi + ">" );
};
// format an element with content
// content is assumed to be well-formed markup
var formatElement = function(gi, attlist, content)
{
return( formatStag(gi, attlist) + content + formatEtag(gi) + "\n" );
};
// format an element with complex content
// contentobj is assumed to be a JavaScript object which optionally implements a formatXML method
// if present formatXML is assumed to return well formed markup
var formatElementTree = function(gi, attlist, contentobj)
{
var data = formatStag(gi, attlist);
if (contentobj.formatXML)
{
data = data + contentobj.formatXML() + "\n";
}
return( data + formatEtag(gi) );
};
// format a list of elements
// contentobj is assumed to be a JavaScript array of objects that implement a formatXML method
var formatElementList = function(gi, itemenclosure, attlist, contentobj)
{
var data = formatStag(gi, attlist);
for (var i=0; i<contentobj.length; ++i) {
data = data + contentobj[i].formatXML(itemenclosure) + "\n";
}
return( data + formatEtag(gi) );
};
// default formatXML
var dflt_formatXML = function(enclosure)
{
var data = "";
for (var prop in this)
{
if ( this.hasOwnProperty(prop) ) {
data = data + formatStag("property") + formatElement("key", null, prop );
if ( Object.prototype.toString.apply(this[prop]) === '[object Array]' ) {
console.log("Formatting list");
data = data + formatElementList("value", "item", null, this[prop] );
} else if ( typeof this[prop] == "object" ) {
data = data + this[prop].formatXML("value");
}
else {
data = data + formatElement("value", null, this[prop]);
}
data = data + formatEtag("property");
}
}
if (enclosure) {
data = formatStag(enclosure)+data+formatEtag(enclosure);
}
return(data);
};
var bindToXML = function( constructor ) {
constructor.prototype.formatXML = dflt_formatXML;
return( constructor );
};
bindToXML(Object);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment