/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function ResolveDependency( value:String, data:XMLList ):String { var pattern:RegExp = /\{([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?\}/g; var patternNodes:RegExp = /([a-zA-Z]([\w\_\-])*)(([\.][a-zA-Z]([\w\_\-])*)*([\.](@?)[a-zA-Z]([\w\_\-])*))?/g; var patternAttribute:RegExp = /\@/; var list:Array; var node:XMLList; var vnode:String; var attribute:String = null; var lnode:Array = value.match( pattern ); if ( lnode != null && lnode.length > 0 ) { for ( var a:uint = 0; a < lnode.length; a++ ) { node = data; vnode = String( lnode[a] ); list = String( vnode.match( patternNodes )[0] ).split( "." ); if ( String( list[ list.length - 1 ] ).search( patternAttribute ) > -1 ) { attribute = String( list.pop() ).replace( patternAttribute, "" ); } for ( var b:uint = 0; b < list.length; b++ ) { if ( node.hasOwnProperty( list[b] ) ) { node = node.child( list[b] ); } else if ( b > 0 && ! node.hasOwnProperty( list[b] ) ) { return null; } } if( attribute != null ) { value = value.replace( vnode, node.attribute( attribute ) ); }else{ value = value.replace( vnode, node.text() ); } } return value; } return value; } //Example: var _data:XMLList = new XMLList("<xml><node><value id=\"url\">www.kirikacode.com</value></node></xml>"); trace( ResolveDependency( "http://{node.value}", _data ) ); trace( ResolveDependency( "Type: {node.value.@id}", _data ) );
URL: http://www.kirikacode.com.ar