Показать сообщение отдельно
Старый 11.11.2001, 16:37
john вне форума Посмотреть профиль Отправить личное сообщение для john Посетить домашнюю страницу john Найти все сообщения от john
  № 5  
john
МЕГАФЛЭШЕР

Регистрация: May 1999
Адрес: Россия, Москва
Сообщений: 1,181
вот кину кусочек моей библиотеки для работы с трейсом. в ней есть методы для конвертирования объекта в хмл.

Код:
//*************************************************************************
//*************************************************************************
//
//	display objects in output window (trace function)
//
//*************************************************************************
//*************************************************************************
//-------------------------------------------------------------------------
//	author Evgeniy Potapenko (aka john) john@3wgraphics.com
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
//	convert object to XML
//-------------------------------------------------------------------------

Object.prototype.toXML = function(name,num)
{
	if(name == null)name=typeof(this);
	if(num == null)
	{
		num = 1;
		_root.array_for_delete_numbers = [];
	}
	
	var proto = this.__proto__;
	this.__proto__ = undefined;
	
	var atributes = " #="+"\""+num+".0"+"\"";
	this.number_for_trace_objects = num
	_root.array_for_delete_numbers.push(this);

	if(name == "movieclip")
	{
		atributes += " name=\""+this._name+"\"";
	}
	
	var XML_result = new XML("<"+name+atributes+"><tmp /></"+name+">");
	
	var type , element, marks;
	
	for(var v in this)
	{
		if(v=="__proto__" || v=="number_for_trace_objects" || v=="array_for_delete_numbers")continue;
		
		type = typeof(this[v]);
		
		if(type == "object" || type == "movieclip")
		{
			if(!this[v].number_for_trace_objects)
			{
				if(this[v].__proto__ == Array.prototype)
				{
					type = "array";
				}else if(this[v].__proto__ == XML.prototype){
					type = "XML";
				}else if(this[v].__proto__ == XMLSocket.prototype){
					type = "XMLSocket";
				}else if(this[v].__proto__ == Sound.prototype){
					type = "sound";
				}
				
				element = new XML("<"+v+" />");
				
				if(type=="XML")
				{
					element.firstChild.appendChild(this[v].firstChild.cloneNode(true));
				}else{
					element.firstChild.appendChild(this[v].toXML(type,++num).firstChild);
				}
				
			}else{
				
				this[v].number_for_trace_objects += 0.1;
				atributes = " #="+"\""+this[v].number_for_trace_objects+"\"";
				if(type == "movieclip")
				{
					atributes += " name=\""+this[v]._name+"\"";
				}
				element = new XML("<"+v+atributes+" />");
			}
			
		}else{

			marks=(type=="string")?"\"":"";
			
			if(type=="number"||type=="string"||type=="boolean")
			{
				atributes = "";
			}else{
				atributes = " type=\""+type+"\"";
			}
			
			element = new XML("<"+v+((type=="function")?" type=\"function\"":"")+">"+
			" "+marks+this[v]+marks+" "+"</"+v+">");
			
			if(element.firstChild.firstChild.nodeType==3 && element.firstChild.firstChild.nodeValue=="  ")
			{
				element.firstChild.firstChild.removeNode();
			}
		}
		
		XML_result.firstChild.insertBefore(element.firstChild,XML_result.firstChild.firstChild);
	}
	XML_result.firstChild.lastChild.removeNode();
	
	if(XML_result.firstChild.attributes["#"] == "1.0")
	{
		for (var i=0;i<_root.array_for_delete_numbers.length;i++)
		{
			delete _root.array_for_delete_numbers[i].number_for_trace_objects;
		}
		delete _root.array_for_delete_numbers;
	}
	
	this.__proto__ = proto;
	return XML_result;
}

//-------------------------------------------------------------------------
//	Object
//-------------------------------------------------------------------------

Object.prototype.toString = function()
{
	return this.toXML("Object").toString();
}

//-------------------------------------------------------------------------
//	XML
//-------------------------------------------------------------------------

XML.prototype.__proto__.toString = function(tab)
{
	var traced_value;
	
	if(this.nodeType!=3)
	{
		if(this.nodeName!=null)
		{
			traced_value = "<"+this.nodeName;
			for(var v in this.attributes)
			{
				traced_value+=" "+v+"=\""+this.attributes[v]+"\""
			}
			
			if(this.firstChild==null)
			{
				traced_value+=" />"
			}else{
				if(this.firstChild.nodeType==3 && this.firstChild == this.lastChild)
				{
					traced_value+=">"+this.firstChild.toString()+"</"+this.nodeName+">";
				}else{
					traced_value+=">\r\t"+tab+this.firstChild.toString(tab+"\t")+"\r"+tab+"</"+this.nodeName+">";
				}
			}
		}else{
			traced_value = this.firstChild.toString("");
		}
		
	}else{
		traced_value=this.nodeValue;
	}
	if(this.nextSibling!=null)
	{
		traced_value+="\r"+tab+this.nextSibling.toString(tab);
	}
	
	return traced_value;
}

//-------------------------------------------------------------------------
//	Array
//-------------------------------------------------------------------------

Array.prototype.toString = function()
{
	return this.toXML("Array").toString();
}