A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Targeting childnode attributes

  1. #1
    Member
    Join Date
    Aug 2001
    Posts
    96

    Question Targeting childnode attributes

    Hello

    I am very close to accomplishing this, however i'm about one step away. I am trying to target the childnode attributes of my xml file. Currently, I can successfully target each parentnode but when i try to target the attributes of its childnodes, only the attributes of the firstchild are returned. How can i successfully target each childnode attributes within its parent. Thanks in advance.

    Tone


    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <topics>
    	<general>
    		<item displayName='Cars' dataName='car' filename='vehicles' />
    		<item displayName='Bikes' dataName='bikes' filename='vehicles2' />
    	</general>
    	<sports>
    		<item displayName='Football' dataName='football' filename='helmet' />
    	</sports>
    </topics>

    Code:
    var news:Array;
    var newsXML:XML = new XML();
    newsXML.ignoreWhite = true;
    newsXML.load("test.xml");
    newsXML.onLoad = function() {
    
    	var index:Number = 0;
    
    
    	news = new Array(this.firstChild.childNodes.length);
    	for (var aNode:XMLNode = this.firstChild.firstChild; aNode != null; aNode=aNode.nextSibling) {
    		news[index] = new Array(aNode.childNodes.length);
    
    		for (var subNode:XMLNode = aNode.firstChild; subNode != null; subNode=subNode.nextSibling) {
    			
    			for (attr in subNode.attributes) {
    				news[index][attr] = subNode.attributes[attr];
    			}
    		}
    		index++;
    	}
    	trace(news[1]["filename"]);// vehicles2
    };

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    PHP Code:
    myXML = new XML(); 
    myXML.ignoreWhite = true; 
    myXML.load("news.xml"); 

    myXML.onLoad = function(success) { 
    if (success) parseXML(this); 
    }; 

    function parseXML(xml) { 
    _global.Data = new Array(); 
    aNode = xml.firstChild.firstChild.childNodes; 
    len = aNode.length; 

    for (var n=0;n!=len;n++) { 
    obj = {}; 
    obj.displayName = aNode[n].firstChild.attributes.displayName; 
    obj.dataName = aNode[n].firstChild.attributes.dataName; 
    obj.filename = aNode[n].firstChild.attributes.filename; 
    Data[n] = obj; 

    tracer();     


    function tracer(){ 
    trace(Data[0].filename); // vehicles 
    trace(Data[1].filename); // helmet 
    }; 

    /*--news.xml--

    <?xml version="1.0" encoding="utf-8"?>
    <news>
    <topics>
    <general>
    <item displayName='Cars' dataName='car' filename='vehicles' /> 
    <item displayName='Bikes' dataName='bikes' filename='vehicles2' /> 
    </general>
    <sports>
    <item displayName='Football' dataName='football' filename='helmet' /> 
    </sports>
    <finance>
    <item displayName='Stocks' dataName='stocks' filename='shares' /> 
    <item displayName='Bonds' dataName='bonds' filename='money' /> 
    </finance>
    <health>
    <item displayName='Chiro' dataName='chiro' filename='back' /> 
    <item displayName='Medical' dataName='medical' filename='cure' /> 
    </health>
    </news>
    </topics> 

    */
    ps.. please don't PM these questions to me.
    keep them on the forum for the good of the community

  3. #3
    Member
    Join Date
    Aug 2001
    Posts
    96
    Thanks dog

    Sorry about the pm.

    Great this is half the battle, now how do I target each filename?
    Currently calling:
    Code:
    trace(Data[0].filename); // vehicles
    trace(Data[1].filename); // helmet
    only returns the first filename. in Data[0], there are two, vehicles and vehicles2. Using your code, how do I return vehicles2? Thanks

    tone

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    my bad ..
    to parse all node values to the array and then output the result -
    PHP Code:
    function parseXML(xml) { 
    Data = new Array(); 
    aNode xml.firstChild.firstChild.childNodes
    len aNode.length

    for (var 
    n=0;n!=len;n++) {
    len2 aNode[n].childNodes.length;
    Data[n] = [len2];

    for (var 
    m=0;m!=len2;m++) {
    obj = {}; 
    obj.displayName aNode[n].childNodes[m].attributes.displayName
    obj.dataName aNode[n].childNodes[m].attributes.dataName
    obj.filename aNode[n].childNodes[m].attributes.filename
    Data[n][m] = obj

    }
    tracer();     


    function 
    tracer(){
    for (var 
    n=0;n!=len;n++){
    len3 Data[n].length;
    for (var 
    m=0;m!=len3;m++) {
    trace("Data["+n+"]["+m+"].displayName = "+Data[n][m].displayName);
    trace("Data["+n+"]["+m+"].dataName = "+Data[n][m].dataName);
    trace("Data["+n+"]["+m+"].filename = "+Data[n][m].filename);
    }
    }
    }; 
    hth
    Last edited by a_modified_dog; 11-28-2008 at 05:36 PM.

  5. #5
    Member
    Join Date
    Aug 2001
    Posts
    96
    Sweet, works perfectly. Thanks you very much!!

    tone

  6. #6
    Member
    Join Date
    Aug 2001
    Posts
    96
    For anyone else needing this, you'll need to adjust the aNode from this:

    Code:
    function parseXML(xml) {
    Data = new Array();
    aNode = xml.firstChild.firstChild.childNodes;
    len = aNode.length;
    To this:

    Code:
    function parseXML(xml) {
    	Data = new Array();
    	aNode = xml.firstChild.childNodes;
    	len = aNode.length;

  7. #7
    Member
    Join Date
    Aug 2001
    Posts
    96
    Sorry for the double post. I think this thread is more in line with what i'm attempting to do. I would like to load multiple xml files in flash while still achieving the same results as the previous solution to this post. The 3 loaded xml files all look the same except for different parent nodenames (ie. sports, top, health).

    I've tried by creating a new array but im failing miserably. Please help! Thanks very much.

    tone

    Flash code

    Code:
    xmlFileNames = new Array("top.xml", "sports.xml", "health.xml");
    i = 0;
    Data = new Array();
    listArray = new Array();
    getXML();
    
    function getXML() {
    	_root["objXML"+i] = new XML();
    	_root["objXML"+i].ignoreWhite = true;
    	_root["objXML"+i].load(xmlFileNames[i]);
    	_root["objXML"+i].onLoad = function(success) {
    		listArray.push(this.childNodes);
    		len = listArray.length;
    		if (len == i) {
    			parseXML(listArray);
    		}
    	};
    	i++;
    	if (i<xmlFileNames.length) {
    		getXML();
    	}
    
    }
    
    
    function parseXML(xml) {
    
    	aNode = xml;
    
    	//trace(aNode);
    
    	for (var n = 0; n != len; n++) {
    		len2 = aNode[n].length;
    		Data[n] = [len2];
    		trace(Data[n]);
    
    		for (var m = 0; m != len2; m++) {
    			obj = {};
    			obj.dataName = aNode[n].childNodes[m].attributes.dataName;
    			obj.filename = aNode[n].childNodes[m].attributes.filename;
    			obj.displayName = aNode[n].childNodes[m].attributes.displayName;
    			Data[n][m] = obj;
    		}
    
    	}
    
    }
    
    function tracer() {
    
    	/*len3 = Data[i].length;
    	for (var m = 0; m != len3; m++) {
    	trace("Data["+i+"]["+m+"].dataName = "+Data[i][m].dataName);
    	trace("Data["+i+"]["+m+"].filename = "+Data[i][m].filename);
    	trace("Data["+i+"]["+m+"].displayName = "+Data[i][m].displayName);
    	
    	}*/
    }
    xml file
    PHP Code:
    <general>
            <
    item displayName='Cars' dataName='car' filename='vehicles' />
            <
    item displayName='Bikes' dataName='bikes' filename='vehicles2' />
        </
    general

  8. #8
    Member
    Join Date
    Aug 2001
    Posts
    96
    I cant figure this out to save my life but I think i came close. Please someone help, but trying for 2 days

    Actionscript
    PHP Code:
    xmlFileNames = new Array("top.xml""sports.xml""health.xml");
    0;
    Data = new Array();
    listArray = new Array();
    getXML();

    function 
    getXML() {
        
    _root["objXML"+i] = new XML();
        
    _root["objXML"+i].ignoreWhite true;
        
    _root["objXML"+i].load(xmlFileNames[i]);
        
    _root["objXML"+i].onLoad = function(success) {
            
    parseXML(this);

        };
        

    }

    function 
    parseXML(xml) {
        
    Data = new Array();
        
    aNode xml.firstChild.childNodes;
        
    len aNode.length;
        
    //trace(aNode);


        
    for (var 0!= lenn++) {
            
    len2 aNode[n].childNodes.length;
            
    Data[n] = [len2];

            for (var 
    0!= len2m++) {
                
    obj = {};
                
    obj.displayName aNode[n].childNodes[m].attributes.displayName;
                
    obj.dataName aNode[n].childNodes[m].attributes.dataName;
                
    obj.filename aNode[n].childNodes[m].attributes.filename;
                
    Data[n][m] = obj;
            }
        }
        
    i++;
        if (
    i<xmlFileNames.length) {
            
    getXML();
        }
        
    tracer();

    }

    function 
    tracer() {

        for (var 
    0!= lenn++) {
            
    len3 Data[n].length;
            for (var 
    0!= len3m++) {
                
    trace("Data["+n+"]["+m+"].displayName = "+Data[n][m].displayName;
                
    trace("Data["+n+"]["+m+"].dataName = "+Data[n][m].dataName);
                
    trace("Data["+n+"]["+m+"].filename= "+Data[n][m].filename);
            }
        }

    XML 1
    PHP Code:
    <topics>
        <
    top>
            <
    item displayName='Cars' dataName='car' filename='vehicles' />
            <
    item displayName='Bikes' dataName='bikes' filename='vehicles2' />
        </
    top
    </
    topics
    XML 2
    PHP Code:
    <topics>
       <
    sports>
            <
    item displayName='Cars' dataName='car' filename='vehicles' />
            <
    item displayName='Bikes' dataName='bikes' filename='vehicles2' />
       </
    sports
    </
    topics
    XML 3
    PHP Code:
    <topics>
       <
    health>
            <
    item displayName='Cars' dataName='car' filename='vehicles' />
            <
    item displayName='Bikes' dataName='bikes' filename='vehicles2' />
       </
    health
    </
    topics

  9. #9
    Member
    Join Date
    Aug 2001
    Posts
    96

    Thumbs up

    Ok, to load multiple xml files it will go like this:

    PHP Code:
    xmlFileNames = new Array("top.xml""sports.xml""health.xml");

    aNode = new Array();
    Data = new Array();
    listArray = new Array();
    0;

    getXML();

    function 
    getXML() {
        
    _root["objXML"+i] = new XML();
        
    _root["objXML"+i].ignoreWhite true;
        
    _root["objXML"+i].load(xmlFileNames[i]);
        
    _root["objXML"+i].onLoad = function(success) {

            
    listArray.push(this);
            
    len listArray.length;
            if (
    len == i) {
                for (
    n=0n<=len-1n++) {
                    
    len2 listArray[n].firstChild.firstChild.childNodes.length;
                    
    Data[n] = [len2];

                    for (
    m=0m<=len2-1m++) {
                        
    obj = {};
                        
    obj.displayName listArray[n].firstChild.firstChild.childNodes[m].attributes.displayName;
                        
    obj.dataName listArray[n].firstChild.firstChild.childNodes[m].attributes.dataName;
                        
    obj.filename listArray[n].firstChild.firstChild.childNodes[m].attributes.filename
                        
    Data[n][m] = obj;
                    }
                }
                
    trace(Data[1][0].displayName);
                
    trace(Data[0][1].dataName);
                
    trace(Data[2][0].filename);
            }
        };
        
    i++;
        if (
    i<xmlFileNames.length) {
            
    getXML();
        }


    Im sure it can be done better, but it works for me!

    tone

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