P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
XML
:
change trace() XML (toString)
author:
Evgeniy Potapenko
[+]
,
Submitted: 10.10.01 7a
//Evgeniy Potapeno (aka john) john@3wgraphics.com http://www.3wgraphics.com //This code is written for correct visual reflection of XML while testing (function trace). //By default XML is shown in one line, and it would be nice to see its structure. //By changing the method toString in the XML object, I have changed the reflection. //I think it's not needed to explain the need of this solution. 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{ 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; }
usage
//Evgeniy Potapeno (aka john) john@3wgraphics.com http://www.3wgraphics.com //This code is written for correct visual reflection of XML while testing (function trace). //By default XML is shown in one line, and it would be nice to see its structure. //By changing the method toString in the XML object, I have changed the reflection. //I think it's not needed to explain the need of this solution. //Below - a small example which reflects the changes. //Let's create a simple XML and trace it in usual way. text = "<a><b a='1' b='2' c='3'><c a='1' b='2' c='3'/>aaa<d a='1' b='2' c='3'/></b><c a='1' b='2' c='3'/><d a='1' b='2' c='3'/></a>" anyXML = new XML(text) trace(anyXML); //In the output we see: /* <a><b a="1" b="2" c="3"><c a="1" b="2" c="3" />aaa<d a="1" b="2" c="3" /></b><c a="1" b="2" c="3" /><d a="1" b="2" c="3" /></a> */ //Now let's change the method toString and look for changes. 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{ 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; } trace(" - - - - - new trace - - - - - - - - -"); trace(anyXML); //In the output we see: /* <a> <b a="1" b="2" c="3"> <c a="1" b="2" c="3" /> aaa <d a="1" b="2" c="3" /> </b> <c a="1" b="2" c="3" /> <d a="1" b="2" c="3" /> </a> */ //My congratulations.
msg
1
{
dolcept
[+]
, posted: 07.18.02 12a•-, top
[^]
}
Great idea. That's simple yet invaluable.
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@