1  
     2  /**
     3   * com.sekati.data.XML2Object
     4   * @version 1.0.3
     5   * @author jason m horwitz | sekati.com
     6   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     7   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     8   * 
     9   * Sourced from it.sephiroth.XML2Object, Alessandro Crugnola, 1.0
    10   */
    11  
    12  /**
    13   * return an object with the content of the XML translated<br/>
    14   * note: a node name with "-" will be replaced with "_" for flash compatibility.
    15   * for example <FIRST-NAME> will become FIRST_NAME
    16   * If a node has more than 1 child with the same name, an array is created with the children contents
    17   * 
    18   * {@code The object created will have this structure:
    19   * 	obj {
    20   * 		nodeName : {
    21   * 			attributes : an object containing the node attributes
    22   * 			data : an object containing the node contents
    23   * 		}
    24   * 	}	
    25   * 	Usage:
    26   * 		var data:Object = new XML2Object().parseXML(myXML);
    27   * }
    28   */
    29  class com.sekati.data.XML2Object {
    30  
    31  	private var oResult:Object = new Object( );
    32  	private var oXML:XML;
    33  
    34  	/**
    35  	 * return the xml passed in the parseXML method
    36  	 * {@code Usage: 
    37  	 * 	theXML = XML2Object.xml
    38  	 * }
    39  	 */
    40  	public function get xml():XML {
    41  		return oXML;
    42  	}
    43  
    44  	/**
    45  	 * parse an XMLObject
    46  	 * @param sFile (XML)
    47  	 * @return  Object - the contents of the parsed XML
    48  	 * {@code Usage:
    49  	 * 	XML2Object.parseXML( theXMLtoParse );
    50  	 * }
    51  	 */
    52  	function parseXML(sFile:XML):Object {
    53  		this.oResult = new Object( );
    54  		this.oXML = sFile;
    55  		this.oResult = this.translateXML( );
    56  		return this.oResult;
    57  	}
    58  
    59  	/**
    60  	 * core of the XML2Object class
    61  	 */
    62  	private function translateXML(from:Object, path:Object, name:Object, position:Object):Object {
    63  		var xmlName:String;
    64  		var nodes:Object, node:Object, old_path:Object;
    65  		if (path == undefined) {
    66  			path = this;
    67  			name = "oResult";
    68  		}
    69  		path = path[name];
    70  		if (from == undefined) {
    71  			from = new XML( this.xml.toString( ) );
    72  			from.ignoreWhite = true;
    73  		}
    74  		if (from.hasChildNodes( )) {
    75  			nodes = from.childNodes;
    76  			if (position != undefined) {
    77  				old_path = path;
    78  				path = path[position];
    79  			}
    80  			while (nodes.length > 0) {
    81  				node = nodes.shift( );
    82  				xmlName = node.nodeName;
    83  				if (xmlName != undefined) {
    84  					var __obj__:Object = new Object( );
    85  					__obj__.attributes = node.attributes;
    86  					__obj__.data = node.firstChild.nodeValue;
    87  					if (position != undefined) {
    88  						old_path = path;
    89  					}
    90  					if (path[xmlName] != undefined) {
    91  						if (path[xmlName].__proto__ == Array.prototype) {
    92  							path[xmlName].push( __obj__ );
    93  							name = node.nodeName;
    94  							position = path[xmlName].length - 1;
    95  						} else {
    96  							var copyObj:Object = path[xmlName];
    97  							path[xmlName] = new Array( );
    98  							path[xmlName].push( copyObj );
    99  							path[xmlName].push( __obj__ );
   100  							name = xmlName;
   101  							position = path[xmlName].length - 1;
   102  						}
   103  					} else {
   104  						path[xmlName] = __obj__;
   105  						name = xmlName;
   106  						position = undefined;
   107  					}
   108  				}
   109  				if (node.hasChildNodes( )) {
   110  					this.translateXML( node, path, name, position );
   111  				}
   112  			}
   113  		}
   114  		return this.oResult;
   115  	}
   116  }