dcsimg
A Flash Developer Resource Site

Page 3 of 3 FirstFirst 123
Results 41 to 53 of 53

Thread: 'id' attribute in my XML file - strange things happening

  1. #41
    Junior Member
    Join Date
    Apr 2002
    Posts
    4

    Using this method in functions

    Hi guys,

    I'm experimenting with the id method and I can't get it to work inside a function. please see below code example.


    myXML= new XML ();
    myXML.ignoreWhite = true
    myXML.onLoad=handleLoad;
    myXML.load("menu.xml");

    function handleLoad(status){
    status ? processXML() : trace ("XML not parsed.");
    }

    function processXML(nodeID){
    for (i=0;i < myXML.nodeID.childNodes.length;i++){
    trace (myXML.nodeID.childNodes[i]);
    }
    }

    processXML ("tools");

    If i hard code the nodeID to "tools" for exmple everythig works fine but when passing it as a param in a function it doesn't.

    Any ideas?

    Thanks in advance,
    Splashy

  2. #42
    try using array notation - the example you just wrote will be looking for on object called nodeID in myXML -

    use myXML[nodeID].childNodes etc instead, this should work

    good luck

    P

  3. #43
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112

    Uhh...

    You're asking for the node that is called 'nodeValue'.

    What you want to do is::

    myXML[nodeValue].childNodes

  4. #44
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112

    Uhh...

    lol - exact time!

  5. #45
    great minds etc etc.

  6. #46
    Junior Member
    Join Date
    Apr 2002
    Posts
    4
    Thanks for the help but i still get nothing back.


    myXML= new XML ();
    myXML.ignoreWhite = true
    myXML.onLoad=handleLoad;
    myXML.load("menu.xml");

    function handleLoad(status){
    status ? processXML() : trace ("XML not parsed.");
    }

    function processXML (nodeID){
    for (i=0;i < myXML[nodeID].childNodes.length;i++){
    trace (myXML[nodeID].childNodes[i]);
    }
    }
    processXML ("tools");

    "tools" is the value of id in one of my branches if i hardcode it as shown below.

    function processXML (){
    for (i=0;i < myXML.tools.childNodes.length;i++){
    trace (myXML.tools.childNodes[i]);
    }
    }

    It returns all the children for that branch as expected but even using the method you suggested I get nothing back.

    Boohoo, now I'm really confused!

    Splashy

  7. #47
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112

    Uhh...

    So... why are you calling the function processXML() ??

    Your statically calling the function is not a very good idea since you're not sure if the XML document has fully loaded....

    ... and when you call the processXML() function inside the handleLoad() function, you need to pass a parameter - otherwise it might not work - since you reference a parameter in the processXML() function...

    ... and was it a typo to have two definitions of the same function??

    Try that and see how it goes.

    -Oh ... and why do you say it returns all the children - but then you say you get nothing back??? That was a bit confusing.

  8. #48
    Junior Member
    Join Date
    Apr 2002
    Posts
    4

    No confusion

    The first part of the code is with the array style addressing that you suggested. That doesn't work

    The second part is the function that works by addressing the node directly using a hardcoded value, in this case myXML.tools... tools is the id value of a node in my xml with 3 child nodes. This methods returns the 3 child nodes as expected.

    Grrrr it's hard to explain what i mean.

    Basically I want to pass the value of an id to a function but using myXML.myvarible ... instead of myXML.specificidvalue ... doesn't seem to work.

    cheers for your help guys,
    Splashy

  9. #49
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    OK,

    a couple of things:

    1) First you have no checkes to see if your XML is being loaded or not. What you will want is to check that the XML has been loaded before starting to use it. myXML.onload is called when the XML has finished loaded, which occurs in the background while execution continues. (this probably isn't your problem, but always good to check!)

    2) I think there is a mistake in your processXML function:

    function processXML(nodeID){
    for (i=0;i < myXML[nodeID]childNodes.length;i++){
    trace (myXML[nodeID]childNodes[i]);
    }
    }

    I assume you want to do something with each of the children so that is why I stuck the [i] on the end of the function. Otherwise you will get the same value printed out for each of your child nodes.

    Thanks

    Luke


  10. #50
    Junior Member
    Join Date
    Apr 2002
    Posts
    4
    Hi,
    Sorry tupps you will see earlier in the thread my checks to see that the xml is loaded etc I just didn't include them in the second and third examples, sorry for that.

    Your also quite right I do want to process each of the children, it all appears to be down to me using the dot syntax between the myXML[nodID] and the childNodes.

    Thanks for all your help!!
    Splashy.

  11. #51
    Member
    Join Date
    Jan 2003
    Location
    Australia, Adelaide (SA)
    Posts
    97
    Hi guys,

    Just thought I would post in with a good use for this undocumented feature.

    Often you will get an xml block like the following:
    PHP Code:
    <school>
        <
    teachers>
            <
    teacher id="t1" name="Mary" classes="c1 c2 c4"/>
            <
    teacher id="t2" name="Bob" classes="c3 c5"/>
        </
    teachers>
        <
    classes>
            <class 
    id="c1" name="Math 101" students="s1 s2 s3 s4" teacher="t1"/>
            <class 
    id="c2" name="Flash for beginners" students="s4" teacher="t1"/>
            <class 
    id="c3" name="English" students="s2" teacher="t2"/>
            <class 
    id="c4" name="Computers" students="s2 s4" teacher="t1"/>
            <class 
    id="c5" name="History" students="s3" teacher="t2"/>
        </
    classes>
        <
    students>
            <
    student id="s1" name="Jane" classes="c1 c3 c5"/>
            <
    student id="s2" name="Joe" classes="c3 c4"/>
            <
    student id="s3" name="William" classes="c5"/>
            <
    student id="s4" name="Tim" classes="c1 c2 c4"/>
        </
    students>
    </
    school
    The attribute "classes" on the "teacher" elements (and students/class, classes/student) are IDREFS, the "teacher" attribute of the "class" element is a IDREF.

    These kinds of things can be common in XML to avoid repeating redundant data.

    Using this new id syntax, it's possible to easily get details on each class with something like the following:
    Code:
    var xmlDoc = new XML();
    xmlDoc.ignoreWhite = true;
    xmlDoc.onLoad = showFlashClass;
    xmlDoc.load( 'school.xml' );
    
    function showFlashClass( success )
    {
    	if( success )
    	{
    		var xmlClasses = this.firstChild.childNodes[1].childNodes;
    		for( var i=0; i < xmlClasses.length; i++ )
    		{
    			var xmlClass = xmlClasses[i];
    			var xmlTeacher = this[ xmlClass.attributes[ 'teacher' ] ];
    			var arrStudents = xmlClass.attributes[ 'students' ].split(' ');
    			
    			trace( 'Class: ' + xmlClass.attributes['name'] );
    			trace( '>>Teacher: ' + xmlTeacher.attributes[ 'name'] );
    			trace( '>>Students:' );
    			for( var j=0; j < arrStudents.length; j++ )
    			{
    				var xmlStudent = this[ arrStudents[j] ];
    				trace( '  ' + xmlStudent.attributes[ 'name' ] );
    			}
    		}
    	} else {
    		trace( 'ERROR: unable to load XML!' );
    	}
    }
    The output is:
    Code:
    Class: Math 101
    >>Teacher: Mary
    >>Students:
      Jane
      Joe
      William
      Tim
    Class: Flash for beginners
    >>Teacher: Mary
    >>Students:
      Tim
    Class: English
    >>Teacher: Bob
    >>Students:
      Joe
    Class: Computers
    >>Teacher: Mary
    >>Students:
      Joe
      Tim
    Class: History
    >>Teacher: Bob
    >>Students:
      William
    Hope this helps!

    EDIT: fixed REFID and REFIDS to IDREF and IDREFS, thanks Vaykent
    Last edited by XMLEvangelist; 01-19-2003 at 07:32 PM.

  12. #52
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112
    That actually does help.

    ... and I swear - I'm not trying to be picky... it's IDREF not REFID

    Thanks for your comments!
    Richard Lyman
    rich at lithinos.com
    www.lithinos.com

  13. #53
    Senior Member random10122's Avatar
    Join Date
    Mar 2002
    Location
    Sheffield, UK
    Posts
    1,747
    Just thought i would tag a quick thankyou note on the end, this thread has sorted out all my problems! Why on earth is this not in the help files?!

    Thanks a lot.

    fracture2 - the sequel
    fracture - retro shooter
    blog - games, design and the rest

    "2D is a format, not a limitation" -Luis Barriga

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