[cc lang=”actionscript3″]
/*
************************************************************
Developed by R.Arul Kumaran [
[email protected]] *
for more code keep visiting [www.shockwave-india.com/blog] *
************************************************************
version 1.0 Last updated on 4 July, 2006
*/
/*
XMLHighlighter for ActionScript3 generates color highlighted pretty printed HTML code
*/
package
{
import flash.xml.*
class XMLDocumentHighlighter
{
public static var useCDATA : Boolean = true;
//_c: color string list
private static var _c : Object = {
tag : ‘0000FF’, att : ‘FF0000’, txt : ‘000000’, tgt : ‘990000’
};
//_fTag: get font begin tag
private static function _fTag (clr : String, content : String)
{
return “
” + content;
}
// _tf: font tag close string
private static var _tf : String = ““;
// public function
public static function highlight (x : XMLDocument) : String
{
//var x:XML;
var ignoreWhite = x.ignoreWhite;
var s : String = _fTag (_c.tag, ”);
x.ignoreWhite = true;
if (x.nodeName == undefined)
{
if (x.xmlDecl != undefined)
{
s += x.createTextNode (x.xmlDecl) + ‘r’;
}
if (x.docTypeDecl != undefined)
{
s += x.createTextNode (x.docTypeDecl) + ‘r’;
}
s += _getHStr (x.firstChild, ”, ”);
} else
{
s += _getHStr (x, ”, ”);
}
x.ignoreWhite = ignoreWhite;
return “
" + s + _tf + "
“;
}
private static function _getHStr (x, tab : String, r : String) : String
{
var s : String = ”;
switch (x.nodeType)
{
//TEXT_NODE
case 3 :
//Is it a CDATA Node?
var xt : String = x.toString ();
if (useCDATA && (x.nodeValue.indexOf (‘<') != - 1 || x.nodeValue.indexOf ('>‘) != – 1))
{
s = “<![CDATA[” + _fTag (_c.txt, ‘‘ + xt + ‘‘) + _tf + “]]>”;
} else
{
s = _fTag (_c.txt, ‘‘ + xt.split (‘&’).join (‘&’) + ‘‘) + _tf;
}
break;
//ELEMENT_NODE
case 1 :
default :
s = r + tab + ‘<‘ + _fTag (_c.tgt, x.nodeName) + _tf;
for (var v in x.attributes)
{
s += _fTag (_c.tgt, ” ” + v) + _tf + “="” + _fTag (_c.att, x.attributes [v]) + _tf + “"”;
}
if (x.firstChild == null)
{
s += “/>”;
} else
{
s += “>” + _getHStr (x.firstChild, tab + “t”, “r”);
if (x.lastChild.nodeType == 3)
{
s += “</”;
} else
{
s += ‘r’ + tab + “</”;
}
s += _fTag (_c.tgt, x.nodeName) + _tf + “>”;
}
}
if (x.nextSibling != null)
{
s += _getHStr (x.nextSibling, tab, r);
}
return s;
}
}
}
/*
//Usage:-
//copy this as file to the same folder as your FLA
//Usage Example:
var my_xml:XMLDocument=new XMLDocument(‘‘);
trace(XMLDocumentHighlighter.highlight(my_xml));
//trace output :
<a>
<b>
<c/>
</b>
</a>
*/
[/cc]