dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: XML Help with mutli-vars

  1. #1
    Senior Member
    Join Date
    May 2003
    Posts
    160

    XML Help with mutli-vars

    ok, I have a program that works like this ... I want to set multiuple vars with different elements from an XML doc ...

    here is the code from flash
    __________________________________________
    grad_xml = new XML();
    grad_xml.onLoad = Start;
    grad_xml.load("graduates.xml");
    grad_xml.ignoreWhite = true;

    var test = new Array('compName', 'compAddress', 'compPhone', 'compSite');
    compName = test.toString();
    // Intialize variables

    function Start(success) {
    if (success == true) {

    rootNode = grad_xml.firstChild; // References xml doc tree

    firstClass = rootNode.firstChild; // References first class

    totalClasses = rootNode.childNodes.length; // Counts amount of classes

    currentClass = firstClass;


    nodeSize = rootNode.childNodes[0].childNodes.length

    for (i=0; i<nodeSize; i++) {
    test[i] = rootNode.childNodes[0].childNodes[i].attributes.desc
    }
    }
    }
    __________________________________________

    Here is the XML doc
    __________________________________________

    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>

    <classes>

    <class state="NJ">
    <text desc="NJ Performance"></text>
    <text desc="115 Route 46"></text>
    <text desc="201-GO-FAST1"></text>
    <text desc="www.njperformance.com"></text>
    </class>

    </classes>
    __________________________________________
    I thought I could set a the vars into any array and set them dynamically though the for loop ... but that just sets the index of the array ... is there a way I can do this ... Do you know what I mean ... If not I could explan it in more detail, if needed ....

    thanks guys,

    -s

  2. #2
    Retired Mod aversion's Avatar
    Join Date
    Jun 2000
    Location
    insomnia
    Posts
    7,917
    hey man, I think some code is missing from your post, your loop is screwed up for one, probably because the forum is interpreting certain characters as html, such as <

    can you edit your post and put your code in [as] or [php] tags as you have with the XML?

    cheers

  3. #3
    Senior Member
    Join Date
    May 2003
    Posts
    160
    code:

    grad_xml = new XML();
    grad_xml.onLoad = Start;
    grad_xml.load("graduates.xml");
    grad_xml.ignoreWhite = true;

    var test = new Array('compName', 'compAddress', 'compPhone', 'compSite');
    compName = test.toString();
    // Intialize variables

    function Start(success) {
    if (success == true) {

    rootNode = grad_xml.firstChild; // References xml doc tree

    firstClass = rootNode.firstChild; // References first class

    totalClasses = rootNode.childNodes.length; // Counts amount of classes

    currentClass = firstClass;


    nodeSize = rootNode.childNodes[0].childNodes.length

    for (i=0; i<nodeSize; i++) {
    test[i] = rootNode.childNodes[0].childNodes[i].attributes.desc
    }
    }
    }



    XML:
    PHP Code:
    <?xml_version="1.0"_encoding="UTF-8"?>

    <classes>

    <class state="NJ">

    <text desc="NJ Performance"></text>

    <text desc="115 Route 46"></text>

    <text desc="201-GO-FAST1"></text>

    <text desc="www.njperformance.com"></text>

    </class>

    </classes>
    There you go ... should work now

  4. #4
    Retired Mod aversion's Avatar
    Join Date
    Jun 2000
    Location
    insomnia
    Posts
    7,917
    your problem isn't really XML related, what you want to be doing is creating an array of named elements.

    when you use a named, or associative, array you can access the named elements two ways.

    code:

    test.compName =
    test.compAddress =
    // etc

    // or
    test["compName"] =
    test["compAddress"] =


    this means they're not very suitable for a loop, in your case you would be better off just writing it out

    code:

    test.compName =rootNode.childNodes[0].childNodes[0].attributes.desc
    test.compAddress =rootNode.childNodes[0].childNodes[1].attributes.desc
    //etc


    if you want to use a loop because you can't predict the length of your subset (in this case the < text> nodes) then you might as well just set your values in the array as the loop is doing in your code now, and just remembering which is which and refering to them in standard array syntax.

    :|

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