Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

toXML.as

Name: Object -> XML; toXML() returns an XML object based on the contents of the current object
Author: senocular: www.senocular.com
Date: 1899-12-31T00:45:11.200
Documentation:
Object TOXML: makes an XML version of your object.
Other objects in the object will be created as new
elements and get the same treatment in conversion.
Strings will become text nodes and all other variables
and properties will become attributes of that object tag.

Arguments:
- _xml: xml node name representing the node the object will become

Returns:
- a new XML object based on the object it was used on

Example:
pirate = {};
pirate.a = "aye";		// will be text node "aye"
pirate.b = 2;		// will be attribute named b with value 2
pirate.c = {d:"argh!"};	// will be new element with a single text node "argh!"

trace(joe.toXML("pirate"));
// output:
argh!aye

1
2
3
4
5
6
7
8
9
10
11
12
13
Object.prototype.toXML = function(_xml){
	if (!arguments.length) return trace("Error: toXML needs atleast one (1) argument.")
	_xml = new XML("<"+_xml+">");
	var type;
	for (var p in this){
		type = typeof this[p];
		if (type == "function") continue;
		if (type == "object") _xml.firstChild.appendChild( this[p].toXML(p) );
		else if (type == "string") _xml.firstChild.appendChild( _xml.createTextNode(this[p]) );
		else _xml.firstChild.attributes[p] = this[p];
	}
	return _xml;
}