A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: getting xml attribute NAMES

  1. #1
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707

    getting xml attribute NAMES

    hi, i have some xml that is full of attributes. Without much detail, i would like to translate the xml into objects. However , i need a method that can get the attribute values AND names. Getting the value is a no brainer, but the names are not coming so easy. For example, the following will translate and xml node into an object with a prop and value of the same:


    PHP Code:
    public function getNodeObject(n:XML) : Object 
            

                 
                var 
    o:Object = new Object
                for 
    each(var m in n.children()) 
                { 
                    var 
    thisNode:XML m  
                    o
    [thisNode.name()] = thisNode.text(); 
                } 
                 
                return 
    o
            } 
    in trying this with attributes, i need to first find a way to get the attribute name. Any ideas?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You get it like this:

    var ms:XML = new XML("<test myName='Hello'/>");
    trace(ms.attributes().name());//trace is myName
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You should be able to use something like this:
    Code:
    public function getNodeObject(n:XML) : Object {
      var o:Object = new Object;
      for each(var m in n.children()){
        var thisNode:XML = m;
        o[thisNode.name()] = thisNode.text();
      }
      for each (var att in n.attributes()){
        o[att.name()] = att.text();
      }
      return o;
    }
    Haven't tried it, just going by the docs.

    You might want to consider making your function recursive.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The name() function works only with one attribute in one node though.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    omg... i can't believe i missed that...thanks guys

  6. #6
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    one last thing, for what ever reason, i had to use att.name().localName; - whatever, i'll take it...thanks

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Here's a recursive version I've been playing around with for a couple of minutes. It handles multiple child nodes named the same.

    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import com.adobe.serialization.json.JSON;
    	
    	/**
    	 * ...
    	 * @author ...
    	 */
    	public class Main extends Sprite 
    	{
    		
    		public function Main():void 
    		{
    			if (stage) init();
    			else addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init(e:Event = null):void 
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			// entry point
    			
    			var testXML:XML = <test name="testname" value="testValue">
    			                    <content id="content1" type="foo">alice</content>
    			                    <content id="content2" type="bar">
    								  <subContent>bob</subContent>
    								</content>
    			                    <content id="content3" type="baz">carol</content>
    							  </test>;
    			
    			trace(JSON.encode(getNodeObject(testXML)));
    		}
    
    		public function getNodeObject(n:XML) : Object {
    		  var o:Object = new Object();
    		  if (n.nodeKind() == "text") {
    			  return n.toString();
    		  }
    		  for each(var m:XML in n.children()){
    			var thisNode:XML = m;
    			if ((thisNode.name() != null) && n.elements(thisNode.name().toString()).length() > 1) {
    				var kids:Array = [];
    				o[thisNode.name().toString()] = kids;
    				for each (var kid:XML in n.elements(thisNode.name().toString())) {
    					kids.push(getNodeObject(kid));
    				}
    			}else {
    				var propName:String;
    				if (thisNode.name() == null) {
    					propName = "__value__";
    				}else {
    					propName = thisNode.name().toString();
    				}
    				
    				o[propName] = getNodeObject(thisNode);
    			}
    		  }
    		  for each (var att:XML in n.attributes()){
    			o["@"+att.name()] = att.toString();
    		  }
    		  return o;
    		}		
    	}
    	
    }
    Output:
    {"@name":"testname","@value":"testValue","content" :[{"__value__":"alice","@id":"content1","@type":"foo "},{"@id":"content2","subContent":{"__value__":"bo b"},"@type":"bar"},{"__value__":"carol","@id":"con tent3","@type":"baz"}]}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center